Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android BaseAdapter應用實例

Android BaseAdapter應用實例

編輯:關於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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved