我的适配器和它的SetAdd类行为很奇怪。
我的gridview包含带有imageview的项目及其上方的通知文字。
gridview使用自定义ArrayList填充。
这是执行此操作的代码:
Items = new ArrayList<SetAdd>();
SetAdd item0 = new SetAdd();
item.setNewNews(5);
item0.setImageID(images[1]);
Items.add(item0);
SetAdd item1 = new SetAdd();
item.setNewNews(3);
item1.setImageID(images[2]);
Items.add(item1);
SetAdd item2 = new SetAdd();
item.setNewNews(4);
item2.setImageID(images[3]);
Items.add(item2);
NewsGV = (GridView) findViewById(R.id.gridView_NEWS);
NewsAdapter adapter = new NewsAdapter(this, Items);
NewsGV.setAdapter(adapter);
这是我的SetAdd类:
public class SetAdd {
public int getImageId() {
return imageId;
}
public void setImageID(int imageId) {
this.imageId = imageId;
}
public int getNewNews() {
return newNews;
}
public void setNewNews(int newNews) {
this.newNews = newNews;
Log.d("setNewNews in SetAdd: ", String.valueOf(newNews));
}
private int imageId;
private int newNews;
}
这是我的适配器类:
公共类NewsAdapter扩展了ArrayAdapter {
private static List<SetAdd> News;
private Activity context;
private LayoutInflater l_inflater;
public NewsAdapter(Activity context, List<SetAdd> News) {
super(context, R.layout.news_grid_view_template, News);
this.context = context;
this.News = News;
}
private class ViewHolder {
ImageView img;
ImageView imgCircle;
TextView txtNewNews;
}
public SetAdd getItem(int position) {
return News.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.news_grid_view_template,
null);
holder = new ViewHolder();
holder.txtNewNews = (TextView) convertView
.findViewById(R.id.txtNewNews);
holder.img = (ImageView) convertView
.findViewById(R.id.img_ImagePlaceHolder);
holder.imgCircle = (ImageView) convertView
.findViewById(R.id.imgCircle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
SetAdd items = (SetAdd) getItem(position);
Log.d("Got New News number is: ", String.valueOf(items.getNewNews()));
//Log.d("Got new Image Id is: ", String.valueOf(items.getImageId()));
if (items.getNewNews() == 0) {
holder.txtNewNews.setVisibility(View.GONE);
holder.imgCircle.setVisibility(View.GONE);
} else {
holder.txtNewNews.setText(String.valueOf(items.getNewNews()));
}
holder.img.setImageResource(items.getImageId());
return convertView;
}
}
现在问题: 我的列表gridview填充了适当的图像,但有0新消息。 我以相同的方式设置imageID和NewNews,但无论我输入什么值,item.setNewNews()都会给出0结果。
答案 0 :(得分:2)
您的代码中存在错误(可能是输入错误)。您永远不会设置item1
和item2
Items = new ArrayList<SetAdd>();
SetAdd item0 = new SetAdd();
item0.setNewNews(5);
item0.setImageID(images[1]);
Items.add(item0);
SetAdd item1 = new SetAdd();
item1.setNewNews(3); // changed this line
item1.setImageID(images[2]);
Items.add(item1);
SetAdd item2 = new SetAdd();
item2.setNewNews(4); // changed this line
item2.setImageID(images[3]);
Items.add(item2);