Android BaseAdapter应用实例

Letitia ·
更新时间:2024-11-15
· 544 次阅读

本文实例讲述了Android BaseAdapter的应用方法。分享给大家供大家参考,具体如下:

直接上源码

/** * 生成联系人Items,即Items中包含如下控件 * */ public final class ViewHolder{ public ImageView callImg;//打电话的图标 public ImageView headImg;//头像 public TextView peopleName;//联系人名 public TextView peopleNumber;//联系人号码 } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } public int getCount() { return listPeople.size(); } public Object getItem(int arg0) { return listPeople.get(arg0); } public long getItemId(int arg0) { return arg0;//当初在看别人例子时,此处返回NUll,加载都没问题,但在ListView中的OnItemsClick事件中就报错了。 } public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.peopleinfo, null); holder.callImg = (ImageView) convertView .findViewById(R.id.callbtn); holder.headImg = (ImageView) convertView .findViewById(R.id.contactImage); holder.peopleName = (TextView) convertView .findViewById(R.id.peopleName); holder.peopleNumber = (TextView) convertView .findViewById(R.id.peopleNumber); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final String name = (String) listPeople.get(position).get( "peopleName"); final String num = (String) listPeople.get(position).get( "peopleNumber"); holder.peopleName.setText((String) listPeople.get(position).get( "peopleName")); holder.peopleNumber.setText((String) listPeople.get(position).get( "peopleNumber")); // holder.headImg.setImageResource(R.drawable.ic_menu_contact); if(listPeople.get(position).get("peoplePhoto") != null){ holder.headImg.setImageBitmap((Bitmap)listPeople.get(position).get( "peoplePhoto")); } holder.callImg.setImageResource(R.drawable.ic_dial_action_call); holder.callImg.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //处理Items中控件的触发事件 } }); return convertView; } } /* *应用BaseAdapter * */ final MyAdapter myAdapter = new MyAdapter(this); contacts.setAdapter(myAdapter); contacts.setOnItemClickListener(new OnItemClickListener() { @SuppressWarnings("unchecked") public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Map<String, Object> map = (Map<String, Object>) myAdapter.getItem(arg2);//此处需要注意, String peopleNumber = String.valueOf(map.get("peopleNumber")); } });

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:学习Android自定义Spinner适配器举例讲解Android应用中SimpleAdapter简单适配器的使用Android编程中常用适配器及自定义适配器用法实例分析Android中 自定义数据绑定适配器BaseAdapter的方法Android列表实现(3)_自定义列表适配器思路及实现代码Android控件系列之相册Gallery&Adapter适配器入门&控件缩放动画入门Android中BaseAdapter的用法分析与理解Android中BaseAdapter用法示例Android之ListView分页加载数据功能实现代码Android之自定义实现BaseAdapter(通用适配器二)



baseadapter Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号