Android 中自定义 Adapter
public classMyListViewAdapter extendsBaseAdapter {privateContext mContext= null;
privateLayoutInflater mLayoutInflater= null;
privateList<Map<String, Object>> mList= null;
//构造方法,参数list传递的就是这一组数据的信息
publicMyListViewAdapter(Context context, List<Map<String, Object>> list) {
this.mList= list;
this.mContext= context;
this.mLayoutInflater= LayoutInflater.from(mContext);
}
//得到总的数量
@Override
public intgetCount() {
// TODOAuto-generated method stub
return this.mList!= null? this.mList.size() : 0;
}
//根据位置得到View对象
@Override
publicObject getItem(intposition) {
// TODOAuto-generated method stub
return this.mList.get(position);
}
//根据ListView位置得到List中的ID
@Override
public longgetItemId(intposition) {
// TODOAuto-generated method stub
returnposition;
}
//根据位置得到View对象
@Override
publicView 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());
returnconvertView;
} www.zzzyk.com
// 定义一个内部类来管理条目中的子组件
public final classViewHolder {
publicImageView imageHead= null;
publicImageView imageContent= null;
publicTextView time= null;
publicTextView name= null;
publicTextView content= null;
publicTextView comment= null;
publicTextView transmit= null;
}
}
补充:移动开发 , Android ,