android – 使用带有ImageViews和TextViews的GridView的问题

前端之家收集整理的这篇文章主要介绍了android – 使用带有ImageViews和TextViews的GridView的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试在每个单元格中使用带有ImageView和TextView的GridView.所以我创建了单元格布局,网格布局,imageAdapter和主要活动,当然,但我不断遇到以下问题:

当我在模拟器上尝试这个时,初始图像和标题会正确显示,但是一旦我向下滚动,一些项目开始变得混乱并且不断变化甚至重复一些时间.

我正在使用2个并行阵列(图像和标题).我尝试使用Log.v函数来查找调用getView时显示的索引和图像,但只能正确分配初始的(可以看到没有滚动).

我通过一遍又一遍地生成视图来解决问题,但这显然不是正确的方法.

这是我正在使用的文件

网格单元格:

网格视图:

GridActivity :(唯一实现的方法)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.menu);

    GridView gridview = (GridView) findViewById(R.id.grdMenu);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView

ImageAdapter:

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Integer[] mThumbIds = {R.drawable.potencia32x32,R.drawable.detalle_cuenta32x32,R.drawable.solicitud32x32,R.drawable.agregar32x32,R.drawable.cyr_32x32,R.drawable.usuarios,R.drawable.cambio_med64x64,R.drawable.cobranza_ex64x64,R.drawable.convenio_pagos64x64,R.drawable.copiabf64x64,R.drawable.info_cliente64x64,R.drawable.mant_exp64x64,R.drawable.ordenes64x64,R.drawable.reembolsos64x64,R.drawable.seguro64x64,R.drawable.solicitudes64x64,R.drawable.suministro64x64

    };
    private String[] Caption = {"Consumo","Facturaciones","Solicitudes","Pagos","Cortes y Rcnx","Datos Generales","Cambios de Medidores","Cobranza Externa","Convenio Pagos","Copia Fac. o Bol.","Info. Cliente","Mant. Expediente","Consulta Ordenes","Historia Reembolsos","Seguros","Caracteristicas Suministro"

    };
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }


    public View getView(int position,View convertView,ViewGroup parent) {


        View myView  = null;

        if(convertView==null)
        {
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            myView = li.inflate(R.layout.grdmenu_cell,null);

            TextView tv = (TextView) myView.findViewById(R.id.grid_item_text);
            Log.v("D:<",String.valueOf(Caption.length) +" y: "+ String.valueOf(position));
            tv.setText(Caption[position]);


            ImageView iv = (ImageView) myView.findViewById(R.id.grid_item_image);
            Log.v("D:<",String.valueOf(mThumbIds.length) +" y: "+ String.valueOf(position));
            iv.setImageResource(mThumbIds[position]);
        }
        else
        {
            myView = convertView;
        }

        return myView;
    }

}

这是我的第一个问题,如果我犯了任何错误,请告诉我.提前致谢.

最佳答案
我认为错误发生在你的适配器的getView中. ConvertView不会保留通过findViewById()和其他方式访问的资源,只有膨胀的视图.尝试将其更改为以下内容

public View getView(int position,ViewGroup parent) {


    View myView  = null;

    if(convertView==null)
    {
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myView = li.inflate(R.layout.grdmenu_cell,null);
    }else{
        myView = convertView;
    }


    TextView tv = (TextView) myView.findViewById(R.id.grid_item_text);
    Log.v("D:<",String.valueOf(Caption.length) +" y: "+ String.valueOf(position));
    tv.setText(Caption[position]);

    ImageView iv = (ImageView) myView.findViewById(R.id.grid_item_image);
    Log.v("D:<",String.valueOf(mThumbIds.length) +" y: "+ String.valueOf(position));
    iv.setImageResource(mThumbIds[position]);

    return myView;

}

原文链接:https://www.f2er.com/android/430543.html

猜你在找的Android相关文章