Android開發之簡單通訊錄(BaseAdapter)
讓ListView更強大的BaseAdapter
- 上一篇文章中的例子我們使用了ArrayAdapter作為資料與ListView的橋樑,完成將資料載入到ListView的功能。但展示出來的列表(子項)資料顯得太過單調,沒有一個子項該有的複雜。例如我們想要一個子項顯示一個聯絡人資訊,那它必然包含頭像、姓名、聯絡電話等詳細資訊。那我們如何實現這一功能呢,答案很明顯, 我們需要用到BaseAdapter來自定義一個介面卡。
BaseAdapter是什麼?
-
BaseAdapter是Android應用程式中經常用到的基礎資料介面卡的基類,它實現了Adapter介面。它可以將一組資料傳到ListView顯示元件進行顯示。使用BaseAdapter主要是通過繼承此類來實現BaseAdapter的四個方法:
public int getCount(): 介面卡中資料集的資料個數。
public Object getItem(int position): 獲取資料集中與索引對應的資料項。
public long getItemId(int position): 獲取指定行對應的ID。
public View getView(int position,View convertView,ViewGroup parent): 獲取每一行Item的顯示內容。
利用BaseAdapter完成簡單通訊錄
- 我們準備完成的功能:
- 建立自定介面卡MyAdapter繼承自BaseAdapter,重寫上述四個方法。在getView()方法中繫結佈局,設定內容。
- 在主活動頁面顯示已定義好聯絡人資料。
- 可以手動增加聯絡人到列表中。
-
建立自定義介面卡MyAdapter。
關鍵程式碼如下:
public class MyAdapter extends BaseAdapter { private LayoutInflater layoutInflater; private Context mcontext; private List<Person> personlist; public MyAdapter (Context context, List<Person> list) { layoutInflater = LayoutInflater.from(context); this.mcontext = context; this.personlist = list; } @Override public int getCount() { return personlist.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = layoutInflater.inflate(R.layout.person_information, null); } else{ view = convertView; } //繫結佈局 TextView textView = (TextView)view.findViewById(R.id.text_name); TextView textView1 = (TextView)view.findViewById(R.id.text_phonenumber); ImageView imageView = (ImageView)view.findViewById(R.id.image); //設定內容 textView.setText(personlist.get(position).getName()); textView1.setText(personlist.get(position).getPhonenum()); imageView.setImageResource(personlist.get(position).getImageId()); return view; } }
一些說明
-
public MyAdapter (Context context, List<Person> list)
這是MyAdapter的有參構造方法,用於獲取獲取context和list物件。其中第一個引數傳入上下文,第二個引數傳入適配資料集合,這裡我們傳入一個List集合(其中包含聯絡人資料)。
LayoutInflater是自定義介面卡常用的一個類,它的作用類似於findViewById()。不同點是LayoutInflater是用來找layout下的xml佈局檔案,並且例項化。 -
public View getView(int position, View convertView, ViewGroup parent)
用來獲取每一行Item所需的資料。
-
第二個功能和第三個功能我們放在一起實現,他們都定義在MainActivity中。
定義一些初始化聯絡人資料,程式碼如下:
private void getData() { Person p1 = new Person("jjj", "223253", R.drawable.banana_pic); Person p2 = new Person("高老闆", "12534315", R.drawable.apple_pic); Person p3 = new Person("蛤", "23454353", R.drawable.mango_pic); Person p4 = new Person("hk", "223245342", R.drawable.strawberry_pic); Person p5 = new Person("齊", "46543", R.drawable.pear_pic); Person p6 = new Person("ppn", "456546", R.drawable.banana_pic); personList.add(p1); personList.add(p2); personList.add(p3); personList.add(p4); personList.add(p5); personList.add(p6); }
點選按鈕,新增聯絡人到ListView列表中,程式碼如下:
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = editText1.getText().toString(); String number = editText2.getText().toString(); Person p7 = new Person(name, number, R.drawable.pineapple_pic); personList.add(p7); } });
一些說明
Person p1 = new Person("jjj", "223253", R.drawable.banana_pic); personList.add(p1);
我們來看一看具體實現效果
-
初始介面如下:
初始化介面
-
新增聯絡人成功後:
新增聯絡人
Github地址
- 主佈局程式碼
ofollow,noindex">https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/res/layout/activity_main.xml - 子項佈局程式碼
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/res/layout/person_information.xml - 主活動程式碼
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/java/com/example/addressbook/MainActivity.java - 自定義介面卡程式碼
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/java/com/example/addressbook/MyAdapter.java - 自定義Person類程式碼
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/java/com/example/addressbook/Person.java