先给条目布局添加Tag标签,记录在控件中保存的数据,BaseAdapter的继承类的getViw()方法重写如下
/**
* ListView Item设置
*/
public View getView(int position, View convertView, ViewGroup parent) {
//Log.d("method", "getView");
//自定义视图
ListItemView listItemView = null;
if (convertView == null) {
//获取list_item布局文件的视图
convertView = listContainer.inflate(this.itemViewResource, null);
listItemView = new ListItemView();
//获取控件对象
listItemView.title = (TextView)convertView.findViewById(R.id.news_listitem_title);
listItemView.author = (TextView)convertView.findViewById(R.id.news_listitem_author);
listItemView.count= (TextView)convertView.findViewById(R.id.news_listitem_commentCount);
listItemView.date= (TextView)convertView.findViewById(R.id.news_listitem_date);
listItemView.flag= (ImageView)convertView.findViewById(R.id.news_listitem_flag);
//设置控件集到convertView
convertView.setTag(listItemView);
}else {
listItemView = (ListItemView)convertView.getTag();
}
//设置文字和图片
News news = listItems.get(position);
listItemView.title.setText(news.getTitle());
listItemView.title.setTag(news);//设置隐藏参数(实体类)
return convertView;
}
static class ListItemView{ //自定义控件集合
public TextView title;
public TextView author;
public TextView date;
public TextView count;
public ImageView flag;
}
在列表的点击事件中,先找到设置了标签的控件,即可得到存在其中的数据
lvNews.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
News news=null;
TextView tv=(TextView)view.findViewById(R.id.news_listitem_title);
news=(News)tv.getTag();
}
});