GridView的Android ImageAdapter显示相同(加倍)的图像
我的问题很简单:当我点击显示GridView(一切都很好)时,我按下后退键,然后再次点击GridView。然后在GridView中有两个图像。例如,如果GridView中第一次有5张照片,第二次有10张照片。我认为问题在于我的ImageAdapter:GridView的Android ImageAdapter显示相同(加倍)的图像,android,image,gridview,Android,Image,Gridview,我的问题很简单:当我点击显示GridView(一切都很好)时,我按下后退键,然后再次点击GridView。然后在GridView中有两个图像。例如,如果GridView中第一次有5张照片,第二次有10张照片。我认为问题在于我的ImageAdapter: public class ImageAdapter extends BaseAdapter { private List<Item> items = new ArrayList<Item>(); priv
public class ImageAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawable;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v;
ImageView picture;
TextView name;
if (view == null) {
v = inflater.inflate(R.layout.squareimageview, null, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = (Item) items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
} else {
v = view;
System.out.println("Cia");
}
return v;
}
}
项目类别:
public class Item {
String name;
int drawable;
String text;
public int getDrawable() {
return drawable;
}
public void setDrawable(int drawable) {
this.drawable = drawable;
}
public Item(String name, String text, int id) {
this.name = name;
this.text = text;
this.drawable = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
返回时,您很可能没有完成活动,因此gridview永远不会被终止,并再次获得相同的一批图像(我相信是在onresume()回调中) 如果是这种情况,请确保完成()覆盖onBackPressed()的活动,或重新创建ListView every onResume()调用: 然后添加数据 然而,为了确定问题,我们需要更多的代码 > 好的,这就是问题所在:
public static List<Item> items = new ArrayList<Item>();
首先,由于未清除用作数据保持器的
ArrayList
,因此会获得双图像
您有一些解决方案:
- 您可以在添加项目之前清除ArrayList
items.clear() items.add(new Item("Name1", "Text1", R.drawable.pic_12)); items.add(new Item("Name2", "Text2", R.drawable.pic_13)); items.add(new Item("Name3", "Text3", R.drawable.pic_14)); items.add(new Item("Name4", "Text4", R.drawable.pic_15)); items.add(new Item("Name5", "Text5", R.drawable.pic_2)); ImageAdapter imaadpter=new ImangeAdapter(this,items); gridView.setAdapter(imaadpter);
- 在添加项目之前,请检查数组中是否有项目
if( items.isEmpty() ) { items.add(new Item("Name1", "Text1", R.drawable.pic_12)); items.add(new Item("Name2", "Text2", R.drawable.pic_13)); items.add(new Item("Name3", "Text3", R.drawable.pic_14)); items.add(new Item("Name4", "Text4", R.drawable.pic_15)); items.add(new Item("Name5", "Text5", R.drawable.pic_2)); } ImageAdapter imaadpter=new ImangeAdapter(this,items); gridView.setAdapter(imaadpter);
- 当您返回时,调用
,那么您肯定不会得到双重项目items.clear()
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v;
ImageView picture;
TextView name;
if (view == null) {
v = inflater.inflate(R.layout.squareimageview, null, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
} else {
v = view;
System.out.println("Cia");
}
Item item = (Item) items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
return v;
}
问题不在这里。。问题是你可以使用这个BaseAdapter。好的,现在看看我的代码。你是否正在清除列表(Items.clear)!!!!??在哪里设置适配器或通知适配器?否。我可以将其放置在哪里?还有别的吗?
public List<Item> items = new ArrayList<Item>();
items.clear();
items.add(new Item("Name1", "Text1", R.drawable.pic_12));
items.add(new Item("Name2", "Text2", R.drawable.pic_13));
items.add(new Item("Name3", "Text3", R.drawable.pic_14));
items.add(new Item("Name4", "Text4", R.drawable.pic_15));
items.add(new Item("Name5", "Text5", R.drawable.pic_2));
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this, items));
items.clear()
items.add(new Item("Name1", "Text1", R.drawable.pic_12));
items.add(new Item("Name2", "Text2", R.drawable.pic_13));
items.add(new Item("Name3", "Text3", R.drawable.pic_14));
items.add(new Item("Name4", "Text4", R.drawable.pic_15));
items.add(new Item("Name5", "Text5", R.drawable.pic_2));
ImageAdapter imaadpter=new ImangeAdapter(this,items);
gridView.setAdapter(imaadpter);
if( items.isEmpty() ) {
items.add(new Item("Name1", "Text1", R.drawable.pic_12));
items.add(new Item("Name2", "Text2", R.drawable.pic_13));
items.add(new Item("Name3", "Text3", R.drawable.pic_14));
items.add(new Item("Name4", "Text4", R.drawable.pic_15));
items.add(new Item("Name5", "Text5", R.drawable.pic_2));
}
ImageAdapter imaadpter=new ImangeAdapter(this,items);
gridView.setAdapter(imaadpter);
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v;
ImageView picture;
TextView name;
if (view == null) {
v = inflater.inflate(R.layout.squareimageview, null, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
} else {
v = view;
System.out.println("Cia");
}
Item item = (Item) items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
return v;
}