自己做个笔记,方便以后的查阅,欢迎点评学习!
//声明ListView
private ListView lv_dept;
//加载ListView
lv_dept = (ListView) findViewById(R.id.lv_dept);
//设置Adapter
//depts 为实体类的list集合
lv_dept.setAdapter(new ListViewAdapter(this,depts));
//自定义的Adapter
class ListViewAdapter extends BaseAdapter{
//布局加载器
LayoutInflater mInflater;
//实体类集合 private List<Depart> list; //context //list public ListViewAdapter(Context context, List list) { this.list=list; mInflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView==null){ viewHolder = new ViewHolder();
//加载自定义的布局文件 convertView=mInflater.inflate(R.layout.dept_item,null); viewHolder.txtId= (TextView) convertView.findViewById(R.id.tv_DeptId); viewHolder.txtName= (TextView) convertView.findViewById(R.id.tv_DeptName); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.txtId.setText(list.get(position).getAccessDeptId()); viewHolder.txtName.setText(list.get(position).getDeptName()); return convertView; } }
//ViewHolder 自定义ViewHolder类 class ViewHolder{ TextView txtId; TextView txtName; }
仅供参考,欢迎指导!!!