public classMyListViewAdapter extends BaseAdapter {
private Context mContext= null;
private LayoutInflater mLayoutInflater= null;
private List<Map<String, Object>> mList= null;
//构造方法,参数list传递的就是这一组数据的信息
public MyListViewAdapter(Context context, List<Map<String, Object>> list) {
this.mList= list;
this.mContext= context;
this.mLayoutInflater= LayoutInflater.from(mContext);
}
//得到总的数量
@Override
public int getCount() {
// TODOAuto-generated method stub
return this.mList!= null? this.mList.size() : 0;
}
//根据位置得到View对象
@Override
public Object getItem(intposition) {
// TODOAuto-generated method stub
return this.mList.get(position);
}
//根据ListView位置得到List中的ID
@Override
public long getItemId(intposition) {
// TODOAuto-generated method stub
returnposition;
}
//根据位置得到View对象
@Override
public View getView(intposition, View convertView, ViewGroup parent) {
// TODOAuto-generated method stub
//得到条目中的子组件
ViewHolder holder = null;
if(convertView == null) {
holder = newViewHolder();
convertView = mLayoutInflater.inflate(R.layout.activity_main_item, null);
holder.imageHead= (ImageView) convertView.findViewById(R.id.imageHead);
holder.name= (TextView) convertView.findViewById(R.id.name);
holder.time= (TextView) convertView.findViewById(R.id.time);
holder.content= (TextView) convertView.findViewById(R.id.content);
holder.imageContent= (ImageView) convertView.findViewById(R.id.imageContent);
holder.transmit= (TextView) convertView.findViewById(R.id.transmit);
holder.comment= (TextView) convertView.findViewById(R.id.comment);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
//对ListView的Item中的控件的操作
holder.imageHead.setImageResource((Integer) mList.get(position).get("imageHead"));
holder.name.setText(mList.get(position).get("name").toString());
holder.time.setText(mList.get(position).get("time").toString());
holder.content.setText(mList.get(position).get("content").toString());
holder.imageContent.setImageResource((Integer) mList.get(position).get("imageContent"));
holder.transmit.setText(mList.get(position).get("transmit").toString());
holder.comment.setText(mList.get(position).get("comment").toString());
return convertView;
}
// 定义一个内部类来管理条目中的子组件
public final classViewHolder {
public ImageView imageHead= null;
public ImageView imageContent= null;
public TextView time= null;
public TextView name= null;
public TextView content= null;
public TextView comment= null;
public TextView transmit= null;
}
}
Android 中自定义 Adapter
最新推荐文章于 2021-05-25 15:18:09 发布