Android之Adapter用法總結

1.概念

        Adapter是連接後端數據和前端顯示的適配器接口,是數據和UI(View)之間一個重要的紐帶。在常見的View(ListView,GridView)等地方都需要用到Adapter。如下圖直觀的表達了Data、Adapter、View三者的關係:

Android中所有的Adapter一覽:

        由圖可以看到在Android中與Adapter有關的所有接口、類的完整層級圖。在我們使用過程中可以根據自己的需求實現接口或者繼承類進行一定的擴展。比較常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。

  • BaseAdapter是一個抽象類,繼承它需要實現較多的方法,所以也就具有較高的靈活性;
  • ArrayAdapter支持泛型操作,最爲簡單,只能展示一行字。
  • SimpleAdapter有最好的擴充性,可以自定義出各種效果。
  • SimpleCursorAdapter可以適用於簡單的純文字型ListView,它需要Cursor的字段和UI的id對應起來。如需要實現更復雜的UI也可以重寫其他方法。可以認爲是SimpleAdapter對數據庫的簡單結合,可以方便地把數據庫的內容以列表的形式展示出來。

2.應用案例

1)ArrayAdapter

列表的顯示需要三個元素:

a.ListVeiw 用來展示列表的View。

b.適配器 用來把數據映射到ListView上的中介。

c.數據    具體的將被映射的字符串,圖片,或者基本組件。

案例一

複製代碼
public class ArrayAdapterActivity extends ListActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         //列表項的數據
         String[] strs = {"1","2","3","4","5"};
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strs);
         setListAdapter(adapter);
     }
 }
複製代碼

案例二

複製代碼
    public class MyListView extends Activity {
    
        private ListView listView;
        //private List<String> data = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
             
            listView = new ListView(this);
            listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
            setContentView(listView);
        }
         
        private List<String> getData(){
             
            List<String> data = new ArrayList<String>();
            data.add("測試數據1");
            data.add("測試數據2");
            data.add("測試數據3");
            data.add("測試數據4");
             
            return data;
        }
    }
複製代碼

        上面代碼使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數據,要裝配這些數據就需要一個連接ListView視圖對象和數組數據的適配器來兩者的適配工作,ArrayAdapter的構造需要三個參數,依次爲this,佈局文件(注意這裏的佈局文件描述的是列表的每一行的佈局,android.R.layout.simple_list_item_1是系統定義好的佈局文件只顯示一行文字,數據源(一個List集合)。同時用setAdapter()完成適配的最後工作。效果圖如下:

2)SimpleAdapter
  simpleAdapter的擴展性最好,可以定義各種各樣的佈局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(複選框)等等。下面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差別,不同就是對顯示ListView做了許多優化,方面顯示而已。

案例一

simple.xml

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textColor="#ffffff"
android:textSize="20sp"
/>
</LinearLayout>
複製代碼
複製代碼
public class SimpleAdapterActivity extends ListActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         
         SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[] { "title",  "img" }, new int[] { R.id.title, R.id.img });
         setListAdapter(adapter);
     }
     
     private List<Map<String, Object>> getData() {
         //map.put(參數名字,參數值)
         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("title", "摩托羅拉");
         map.put("img", R.drawable.icon);
         list.add(map);
         
         map = new HashMap<String, Object>();
         map.put("title", "諾基亞");
         map.put("img", R.drawable.icon);
         list.add(map);
         
         map = new HashMap<String, Object>();
         map.put("title", "三星");
         map.put("img", R.drawable.icon);
         list.add(map);
         return list;
         }  
     
 }
複製代碼

案例二
  下面的程序是實現一個帶有圖片的類表。首先需要定義好一個用來顯示每一個列內容的xml,vlist.xml

複製代碼
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="fill_parent">   
        <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/>
        <LinearLayout android:orientation="vertical"  android:layout_width="wrap_content"  android:layout_height="wrap_content">
            <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF" android:textSize="22px" />
            <TextView android:id="@+id/info"  android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF" android:textSize="13px" />
        </LinearLayout>
     </LinearLayout>
複製代碼
複製代碼
public class MyListView3 extends ListActivity {
        // private List<String> data = new ArrayList<String>();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
            SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
                    new String[]{"title","info","img"},
                    new int[]{R.id.title,R.id.info,R.id.img});
            setListAdapter(adapter);
        }
     
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
     
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
     
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
     
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
             
            return list;
        }
    }
複製代碼

  使用simpleAdapter的數據用一般都是HashMap構成的List,list的每一節對應ListView的每一行。HashMap的每個鍵值數據映射到佈局文件中對應id的組件上。因爲系統沒有對應的佈局文件可用,我們可以自己定義一個佈局vlist.xml。下面做適配,new一個SimpleAdapter參數一次是:this,佈局文件(vlist.xml),HashMap的 title 和 info,img。佈局文件的組件id,title,info,img。佈局文件的各組件分別映射到HashMap的各元素上,完成適配。

運行效果如下圖:

3)SimpleCursorAdapter

複製代碼
public class SimpleCursorAdapterActivity extends ListActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         //獲得一個指向系統通訊錄數據庫的Cursor對象獲得數據來源
         Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
         startManagingCursor(cur);
         //實例化列表適配器
         
         ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {People.NAME}, new int[] {android.R.id.text1});
         setListAdapter(adapter);
     }
 }
複製代碼

一定要以數據庫作爲數據源的時候,才能使用SimpleCursorAdapter,這裏特別需要注意的一點是:不要忘了在AndroidManifest.xml文件中加入權限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

效果如下:

4)BaseAdapter

  有時候,列表不光會用來做顯示用,我們同樣可以在在上面添加按鈕。添加按鈕首先要寫一個有按鈕的xml文件,然後自然會想到用上面的方法定義一個適配器,然後將數據映射到佈局文件上。但是事實並非這樣,因爲按鈕是無法映射的,即使你成功的用佈局文件顯示出了按鈕也無法添加按鈕的響應,這時就要研究一下ListView是如何現實的了,而且必須要重寫一個類繼承BaseAdapter。下面的示例將顯示一個按鈕和一個圖片,兩行字如果單擊按鈕將刪除此按鈕的所在行。並告訴你ListView究竟是如何工作的。

vlist2.xml

複製代碼
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/>
        <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
           <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF" android:textSize="22px" />
           <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor="#FFFFFFFF" android:textSize="13px" />
       </LinearLayout>

       <Button android:id="@+id/view_btn" android:layout_width="wrap_content"  android:layout_height="wrap_content"
            android:text="@string/s_view_btn" android:layout_gravity="bottom|right" />
    </LinearLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章