0
我的適配器及其SetAdd類很奇怪。使用自定義列表視圖時的奇怪行爲
我的gridview包含帶有imageview的項目和上面的通知文本。
使用自定義ArrayList的gridview填充。
這裏是代碼做到這一點:
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()給出的結果不管我輸入什麼值。
謝謝,我修好了。這樣一個小問題。愚蠢的我沒有注意到這個錯字。 –