上回說到跟ListView相關的Adapter,這次就來說說ListView。其實,這兩者不應該分開說的,應該寫到一篇文章裡面。但是我就是要分開寫,不服你來打我呀。/手動斜眼
在安卓中,用的最多的控件或者說最難用的控件就是ListView了。微信聯系人,刷朋友圈,刷微網誌這些都是listView。我打賭目前以我這個戰五渣的水準,listView我肯定寫不好,沒有關系,先挖坑,日後再填。不服你再打我呀?/手動滑稽
ListView三要素:
1.ListVeiw 用來展示清單的View。//今天的重點
2.擴充卡 用來把資料映射到ListView上的中介。//這個我們已經說過了ArrayAdapter了。跟1密不可分
3.資料 具體的将被映射的字元串,圖檔,或者基本元件。//資料就是資料啊,兄弟
根據清單的擴充卡類型,清單分為三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最為簡單,隻能展示一行字。SimpleAdapter有最好的擴充性,可以自定義出各種效果。SimpleCursorAdapter可以認為是SimpleAdapter對資料庫的簡單結合,可以方面的把資料庫的内容以清單的形式展示出來。
我們從最簡單的ListView開始:
ArrayAdapter:
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對象
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
//匿名内部類建立ArrayAdapter擴充卡對象,并擴充卡對象傳遞給listView
//多注釋一句,adapter擴充卡需要三個參數,分别是上下文的this(context),系統預設的隻顯示一行資料的view的ID,data
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()完成适配的最後工作。
SimpleAdapter:
simpleAdapter的擴充性最好,可以定義各種各樣的布局出來,可以放上ImageView(圖檔),還可以放上Button(按鈕),CheckBox(複選框)等等。下面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差别,不同就是對顯示ListView做了許多優化,友善顯示而已。
下面的程式是實作一個帶有圖檔的類表
首先需要定義好一個用來顯示每一個列内容的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的各元素上,完成适配。