服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android BaseAdapter应用实例

Android BaseAdapter应用实例

2021-05-12 16:39hshm20517 Android

这篇文章主要介绍了Android BaseAdapter应用方法,结合生成联系人Items的实例形式分析了BaseAdapter的使用技巧,需要的朋友可以参考下

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

直接上源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* 生成联系人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程序设计有所帮助。

延伸 · 阅读

精彩推荐