listView_v2_系統提供的一些 adapter適配器

上期說道listview 還有些自帶的adapter 這裏給大家稍微介紹兩個

第一個:ArrayAdapter


首先這個ArrayAdapter已經實現了BaseAdapter中的那四個override方法,並且這個適配器中只能有一個TextView組件,大家可能會問爲什麼,我們先看看ArrayAdapter的源代碼

public View getView(int position, View convertView, ViewGroup parent) {
    return createViewFromResource(position, convertView, parent, mResource);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent,
        int resource) {
    View view;
    TextView text;
    if (convertView == null) {
        view = mInflater.inflate(resource, parent, false);
    } else {
        view = convertView;
    }
    try {
        if (mFieldId == 0) {
            //  If no custom field is assigned, assume the whole resource is a TextView
            //arrayadatper直接將view強轉爲TextView
            text = (TextView) view;
        } else {
            //  Otherwise, find the TextView field within the layout
            text = (TextView) view.findViewById(mFieldId);
        }
    } catch (ClassCastException e) {
        Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
        throw new IllegalStateException(
                "ArrayAdapter requires the resource ID to be a TextView", e);
    }
    T item = getItem(position);
    if (item instanceof CharSequence) {
        text.setText((CharSequence)item);
    } else {
        text.setText(item.toString());
    }
    return view;
}



下面 來看看 怎麼用這個ArrayAdapter

第一步:定義一個行佈局

<?xml version="1.0" encoding="utf-8"?>
<!--這就是一個只有TextView 的layout,注意下面那個xmlns必須要有,否則下面的android:layout_width這些屬性就沒了。。。-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="title1"
    >
</TextView>

在main佈局裏面 加入listview 這個跟前面的一樣

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
</RelativeLayout>


第二步:讓MainActivity繼承ListActivity,ListActivity中就有一個ListView,這樣就可以直接使用setListAdapter(adapter)方法加入適配器就可以了

看一下源代碼:


@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //這裏不能有這個了,因爲ListActivity中已經有佈局了,如果非得用自己的佈局,也可以這個後面再說啦~~~
        //setContentView(R.layout.activity_main);
                                                                                                                                                                                                                                                                                                                                                                    
        //首先創建一個 ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_item, mStrings);
                                                                                                                                                                                                                                                                                                                                                                    
        /*參數說明:
         *  context: 要放入當前activity的對象,也就是那個this.
         *  textViewResourceId: 這個要放入一個TextView的資源id,就是 R.layout.list_item
         *  objects: listView的行內容,也就是mStrings.
         **/
                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                    
        //ListActivity中存在一個ListView,直接設置就好
        setListAdapter(adapter);
    }
    private String[] mStrings = {//listview的一些行內容
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
            "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
            "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
            "Aragon", "Ardi Gasna",};

到這裏 ArrayAdapter就算完成了看一下 , 效果吧

嘿嘿 這裏 我遺留了一個問題,我第一篇博客有 說過不知道大家能不能找到

這個問題暫時不會導致系統崩潰,但是以後麻煩很大滴。。。

提示下:關於layout的  嘿嘿,各位同僚 慢慢找撒 O(∩_∩)O哈哈~





第二個:SimpleAdapter

這也是一個系統自帶的adapter,做這個是因爲,ArrayAdapter中只能有一個TextView,多一個都不行,別的組件就更不行了,所以 爲了做多個組件的adapter,就搞了一個SimpleAdapter,爲什麼叫Sipmple,因爲比BaseAdapter簡單唄。。。

下面看步驟:

第一步:同樣先做佈局,這回就可以多搞幾個組件了

代碼太多了看組件樣子吧。。。 佈局用的RelativeLayout,


一個p_w_picpathView,兩個TextView


main的佈局就不用說了吧 同樣一個listview


第二步:mainActivity可以繼承ListActivity,也可以直接繼承Activity,listView就像以前那樣加就好了。

看代碼:

private int mImgResID = R.drawable.ic_launcher;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//設置行內容
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for (int i = 0; i < 5; i++)
        {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("p_w_picpath", mImgResID);
map.put("title", "title"+i);
map.put("time", "time"+i);
data.add(map);
        }
//下面的from和to 是爲了在給佈局中的組件賦值時,指定爲那個組件賦值用的
//from中放的是從map中讀取那個key的值
//to中放的是組件的資源ID
//from的值必須和上面的map中的key一一對應
//同樣to的值也必須跟from裏面的內容一一對應,
String[] from = new String[]{"p_w_picpath","title","time"};
int[] to = new int[]{R.id.p_w_picpathView1,R.id.textView1,R.id.textView2};
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, from, to);
setListAdapter(adapter);
}

這樣simpleAdapter也做好了,看看效果吧


ok今天就到這裏了。。。各位晚安,下週繼續。。。。( ^_^ )/~~拜拜




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章