我正在尝试在每个单元格中使用带有ImageVIEw和TextVIEw的GrIDVIEw.所以我创建了单元格布局,网格布局,imageAdapter和主要活动,当然,但我不断遇到以下问题:
当我在模拟器上尝试这个时,初始图像和标题会正确显示,但是一旦我向下滚动,一些项目开始变得混乱并且不断变化甚至重复一些时间.
我正在使用2个并行阵列(图像和标题).我尝试使用Log.v函数来查找调用getVIEw时显示的索引和图像,但只能正确分配初始的(可以看到没有滚动).
我通过一遍又一遍地生成视图来解决问题,但这显然不是正确的方法.
这是我正在使用的文件:
网格单元格:
网格视图:
GrIDActivity :(唯一实现的方法)
@OverrIDepublic 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> parent,VIEw v,int position,long ID) { Toast.makeText(getApplicationContext(),"" + position,Toast.LENGTH_SHORT).show(); } });}
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;
} 总结
以上是内存溢出为你收集整理的android – 使用带有ImageViews和TextViews的GridView的问题全部内容,希望文章能够帮你解决android – 使用带有ImageViews和TextViews的GridView的问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)