时间:2021-05-21
本文实例讲述了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程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android中BaseAdapter用法。分享给大家供大家参考,具体如下:概述:BaseAdapter就Android应用程序中经常用到的基础数
本文实例讲述了Android桌面组件AppWidget用法。分享给大家供大家参考。具体如下:Android开发应用除了程序应用,还有AppWidget应用。好多
Android照相机的实例应用关键技术:SurfaceHolder.CallbackpublicclassMyCameraDemoextendsActivity
微信聊天窗口的信息效果类似iphone上的短信效果,以气泡的形式展现,在Android上,实现这种效果主要用到ListView和BaseAdapter,配合布局
在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseAdapter显示。B