欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android BaseAdapter应用实例

时间:2020-11-18 01:07:23|栏目:Android代码|点击:

本文实例讲述了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界面布局之实现文本块布局效果示例

栏    目:Android代码

下一篇:Android控件ViewFlipper仿淘宝头条垂直滚动广告条

本文标题:Android BaseAdapter应用实例

本文地址:http://www.codeinn.net/misctech/23892.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有