Android开发之ListView中BaseAdapter的使用

BaseAdapter,官网链接--http://developer.android.com/intl/zh-cn/reference/android/widget/BaseAdapter.html

继承:Object

接口:ListAdapter  SpinnerAdapter

已知直接子类:

ArrayAdapter<T>, CursorAdapter, SimpleAdapter

已知间接子类:

ResourceCursorAdapter, SimpleCursorAdapter

使用BaseAdapter,需要实现4个方法:

getCount()                                                                          --返回需要ListView显示的item数量
getItem(int position)                                                            --返回item的位置
getItemId(int position)                                                         --返回item的id
getView(int position, View convertView, ViewGroup parent)      --显示item的内容

 

--------------------------------------------------------------------------------------------------------------------------------------

实例(创建一个通讯录):

MainActivity.java代码

 1 public class MainActivity extends Activity {
 2 
 3     private ListView phoneList;
 4     private MyAdapter adapter;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         GetPhone.getPhone(MainActivity.this);
11         phoneList = (ListView) findViewById(R.id.phoneList);
12         adapter = new MyAdapter(GetPhone.lists, MainActivity.this);
13         phoneList.setAdapter(adapter);
14     }
15 
16 }
布局文件activity_main.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context="com.lijingbo.getmyphonenumber.MainActivity" >
 7 
 8     <ListView
 9         android:id="@+id/phoneList"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent" >
12     </ListView>
13 
14 </LinearLayout>

获取通讯录中联系人的姓名和号码,GetPhone.java

 1 public class GetPhone {
 2 
 3     static List<PhoneInfo> lists = new ArrayList<PhoneInfo>();
 4 
 5     public static List<PhoneInfo> getPhone(Context context) {
 6 
 7         ContentResolver contentResolver = context.getContentResolver();
 8         Cursor cursor = contentResolver.query(Phone.CONTENT_URI, null, null,
 9                 null, null);
10         while (cursor.moveToNext()) {
11             String number = cursor.getString(cursor
12                     .getColumnIndex(Phone.NUMBER));
13             String name = cursor.getString(cursor
14                     .getColumnIndex(Phone.DISPLAY_NAME));
15             Log.d("getmyphonenumber", name + "电话号码:" + number);
16             PhoneInfo phoneInfo = new PhoneInfo(name, number);
17             lists.add(phoneInfo);
18         }
19         return lists;
20     }
21 }

实体类 PhoneInfo.java

 1 public class PhoneInfo {
 2 
 3     private String name;
 4     private String number;
 5 
 6     public PhoneInfo(String name, String number) {
 7         setName(name);
 8         setNumber(number);
 9 
10     }
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20     public String getNumber() {
21         return number;
22     }
23 
24     public void setNumber(String number) {
25         this.number = number;
26     }
27 
28 }

MyAdapter.java

 1 public class MyAdapter extends BaseAdapter {
 2     
 3     private List<PhoneInfo> lists;
 4     private Context context;
 5     
 6     public MyAdapter(List<PhoneInfo> lists,Context context){
 7         this.lists=lists;
 8         this.context=context;
 9     }
10 
11     @Override
12     public int getCount() {
13         return lists.size();
14     }
15 
16     @Override
17     public Object getItem(int position) {
18         return position;
19     }
20 
21     @Override
22     public long getItemId(int position) {
23         return position;
24     }
25 
26     @Override
27     public View getView(int position, View convertView, ViewGroup parent) {
28         ViewHolder holder=null;
29         if (convertView==null) {
30             holder=new ViewHolder();
31             convertView=LayoutInflater.from(context).inflate(R.layout.phonedetails, null);
32             holder.showName=(TextView) convertView.findViewById(R.id.showName);
33             holder.showNumber=(TextView) convertView.findViewById(R.id.showNumber);
34             convertView.setTag(holder);
35         }else {
36             holder=(ViewHolder) convertView.getTag();
37         }
38         holder.showName.setText(lists.get(position).getName());
39         holder.showNumber.setText(lists.get(position).getNumber());
40         return convertView;
41     }
42     
43     static class ViewHolder{
44         public TextView showName;
45         public TextView showNumber;
46     }
47 
48 }
phonedetails.xml文件
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6 
 7     <TextView
 8         android:id="@+id/showName"
 9         android:layout_width="0dp"
10         android:layout_height="wrap_content" 
11         android:layout_weight="1"
12         android:text="name"/>
13     <TextView
14         android:id="@+id/showNumber"
15         android:layout_width="0dp"
16         android:layout_weight="1"
17         android:layout_height="wrap_content" 
18         android:text="number"/>
19 
20 </LinearLayout>

 

 

 

你可能感兴趣的:(BaseAdapter)