我正在尝试将相同的图像添加到视图中。我有一个定时器,每20分钟就会关闭一次,我想添加一个图像来显示用户已完成一个计时器。
经过一些research我发现我需要使用ArrayAdapter。问题是我不明白这些代码是做什么的。有人可以向我解释一下吗?
到目前为止我发现的是:
protected void onCreate(){
//I need to create a gridview (it could be any type of view)
gridLayout = (GridView) findViewById(R.id.gridLayout);//created in Studio GUI
//Create an image
ImageView myImage = new ImageView(this);
myImage.setImageResource(R.mipmap.ic_launcher);
//Create an image list
private List<ImageView> imgs = new ArrayList<ImageView>();
//add images to list (this will need to be added to the timer later)
for (i = 0; i < numberOfTimersHit; i++){
imgs.add(myImage);
}
//add the list to an ArrayAdapter
//This is where I need help
//add the ArrayAdapter to the grid
gridLayout.setAdapter(imageViewArrayAdapter);
任何帮助将不胜感激。随意纠正我错过的任何事情。
答案 0 :(得分:0)
在我看来,您需要有关实现ImageViewArrayAdapter的帮助,所以方法如下:
public class ImageViewArrayAdapter extends BaseAdapter {
List<ImageView> images;
public ImageViewArrayAdapter(List<ImageView> images) {
this.images = images;
}
@Override
public int getCount() {
return images != null ? images.size() : 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return images.get(position);
}
}
然后在你的代码中使用:
//add the list to an ArrayAdapter
//This is where I need help
ImageViewArrayAdapter imageViewArrayAdapter = new ImageViewArrayAdapter(imgs);
这可能有效。