歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ListView_v2_系統提供的一些 adapter適配器

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

日期:2017/3/1 9:53:59   编辑:Linux編程

前面說了Listvie,(見 adapter適配器 SimpleAdapter介紹見 http://www.linuxidc.com/Linux/2013-08/88904.htm)還有些自帶的adapter 這裡給大家稍微介紹兩個

本文源碼下載

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料/8月/16日/ListView_v2_系統提供的一些 adapter適配器

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

第一個: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哈哈~

adapter適配器 SimpleAdapter介紹見 http://www.linuxidc.com/Linux/2013-08/88905p2.htm

Copyright © Linux教程網 All Rights Reserved