我遇到gridview的问题。我使用了一个非常简单的GridView,其中我有图像和下面的textView。 我的问题是 - 我每行显示3张图像,在我的屏幕上一次显示12张图像,但对于第13张图像,图像是正确的,但下面的文字是第1张图像。 如果我每行使用4张图片(我不想这样做),那么显示的文字是正确的。 在滚动gridview交换的图像和文本之后
这是我的自定义gridview.java代码
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
请尽快回复
答案 0 :(得分:0)
可能你需要更改代码如下。对于第13个项目,转换视图不为null,因此返回旧的第1个视图。
@覆盖 public View getView(int position,View convertView,ViewGroup parent){ // TODO自动生成的方法存根 查看网格; LayoutInflater inflater =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) { grid = new View(mContext); grid = inflater.inflate(R.layout.grid_single, null); TextView textView = (TextView) grid.findViewById(R.id.grid_text); ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image); } else { grid = (View) convertView; } textView.setText(web[position]); imageView.setImageResource(Imageid[position]); return grid;
答案 1 :(得分:0)
这很好用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
} else {
grid = (View) convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
return grid;
}