欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
ListView之一:Adapter介紹與使用

ListView是在A(yíng)ndroid很常見(jiàn)的一種視圖,ListView不論在電話(huà)簿中還是觀(guān)看大量的列表資料,都是會(huì )用得到。

ListView的使用需要搭配Adapter,Adapter是用來(lái)連接資料和ListView的,Adapter除了會(huì )用到ListView中,另外會(huì )用到還有Spinner(類(lèi)似下拉選單)的這個(gè)元件中。在Google IO 2009的其中一張投影片中,可以很清楚的看到其中的關(guān)係圖:
listview1
從這張圖就可以看到Adapter基本上會(huì )分成Cursor類(lèi)和ArrayList類(lèi)的。Cursor和資料庫或目前電話(huà)中的資料有關(guān),例如要抓出目前電話(huà)簿中的聯(lián)絡(luò )人資料,用的就是Cursor的Adapter,而自己在程式中建立的陣列資料,用的就會(huì )是ArrayList類(lèi)的Adapter。
最常用的有幾種Adapter:
  • ArrayAdapter:將一組數組連繫到ListView
  • SimpleAdapter:適用於自訂ListView外觀(guān)的應用場(chǎng)合
  • BaseAdapter:抽象類(lèi)別,所以有多個(gè)方法需要實(shí)作。適用於需要自訂ListView外觀(guān)等靈活應用的場(chǎng)合。
  • SimpleCursorAdapter:將一組現有的資料庫或聯(lián)絡(luò )人等ContentProvider的資料查詢(xún)的結果連繫到ListView中
首先,要使用ListView可以用ListView這個(gè)UI組件,放置到目前的Activity中。另一個(gè)可以用的方式,是直接繼承ListActivity,這是一個(gè)Activity的子類(lèi),其中就會(huì )包含一個(gè)全螢幕的ListView物件。ListActivity用法比較簡(jiǎn)單:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ListDemo1 extends ListActivity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 //注意:不能使用main中的layout,用了會(huì )出現錯誤
 //setContentView(R.layout.main);
 
 setListAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1, mStrings));
 
 //啟用按鍵過(guò)濾功能
 getListView().setTextFilterEnabled(true);
 }
 
 private static final String[] mStrings = new String[] {
 "大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅",
 "豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞",
 "烤香腸","車(chē)輪餅","珍珠奶茶","鹹酥雞","大熱狗",
 "炸雞排","山豬肉","花生冰","剉冰","水果冰",
 "包心粉圓","排骨酥","沙茶魷魚(yú)","章魚(yú)燒","度小月",
 "aaa","abc","bbb","bcd","123"
 };
}

 

第8行:這個(gè)是對照一般的Activity中有的setContentView方法,因為是ListActivity所以不需要,用了也會(huì )有錯誤訊息。
第10行:用setListAdapter方法設定一個(gè)ArrayAdapter
第14行:按鍵過(guò)濾功能,因為L(cháng)ist中的項目有可能很多,像這個(gè)範例你按下鍵盤(pán)中的a,就會(huì )出現以a開(kāi)頭的項目,方便找到項目
第18~25行:一些要放到List中的字串值
假設是在A(yíng)ctivity中除了ListView之外,還要放入其他的組件時(shí),這時(shí)候就需要在A(yíng)ctivity中加入一個(gè)ListView組件,利用這個(gè)組件的setAdapter來(lái)連接Adapter,範例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class ListDemo2 extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 //使用main中的layout
 setContentView(R.layout.main);
 //找到listview組件
 ListView list = (ListView) findViewById(R.id.listView1);
 
 //listview物件使用setAdapter方法(比對ListActivity是用setListAdapter)
 list.setAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1, mStrings));
 
 //啟用按鍵過(guò)濾功能(直接用listview物件,不需要getListView方法)
 list.setTextFilterEnabled(true);
 }
 
 private static final String[] mStrings = new String[] {
 "大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅",
 "豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞",
 "烤香腸","車(chē)輪餅","珍珠奶茶","鹹酥雞","大熱狗",
 "炸雞排","山豬肉","花生冰","剉冰","水果冰",
 "包心粉圓","排骨酥","沙茶魷魚(yú)","章魚(yú)燒","度小月",
 "aaa","abc","bbb","bcd","123"
 };
}

 

值得一提的是在A(yíng)rrayAdapter中有一個(gè)android定義好的內建list樣式 - "android.R.layout.simple_list_item_1",注意這並不是我們定義的,在android系統中預設就有存在了。其中常用的的這些樣式如下所列:
  1. android.R.layout.simple_list_item_1:一行text
  2. android.R.layout.simple_list_item_2:一行text較大,一行text較小
  3. android.R.layout.simple_list_item_single_choice:?jiǎn)芜x
  4. android.R.layout.simple_list_item_multiple_choice:多選按鈕
  5. android.R.layout.simple_list_item_checked:勾選盒
第1個(gè)剛剛有用了,事實(shí)上第3,4,5個(gè)也是直接換上去就可以看到了,只不過(guò)還沒(méi)有實(shí)作選中後的處理方法。
第2個(gè)android.R.layout.simple_list_item_2就比較麻煩了,原因是ArrayAdapter並不支援傳入兩個(gè)字串參數值,所以要改用SimpleAdapter,而且傳入的數值型態(tài)要改為ArrayList才可以,下面的範例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class ListDemo3 extends ListActivity {
 /** Called when the activity is first created. */
 ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
 private SimpleAdapter adapter;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 //把資料加入ArrayList中
 for(int i=0; i<mPlaces.length; i++){
 HashMap<String,String> item = new HashMap<String,String>();
 item.put( "food", mFoods[i]);
 item.put( "place",mPlaces[i] );
 list.add( item );
 }
 
 //新增SimpleAdapter
 adapter = new SimpleAdapter( 
 this, 
 list,
 android.R.layout.simple_list_item_2,
 new String[] { "food","place" },
 new int[] { android.R.id.text1, android.R.id.text2 } );
 
 //ListActivity設定adapter
 setListAdapter( adapter );
 
 //啟用按鍵過(guò)濾功能,這兩行資料都會(huì )進(jìn)行過(guò)濾
 getListView().setTextFilterEnabled(true);
 }
 
 private static final String[] mPlaces = new String[] {
 "臺北市", "新北市", "臺南市", "高雄市", "苗粟縣",
 "臺北市", "新北市", "臺南市", "高雄市", "苗粟縣",
 "臺北市", "新北市", "臺南市", "高雄市", "苗粟縣",
 "臺北市", "新北市", "臺南市", "高雄市", "苗粟縣",
 "臺北市", "新北市", "臺南市", "高雄市", "苗粟縣",
 "臺北市", "新北市", "789", "cde", "abc"
 };
 
 private static final String[] mFoods = new String[] {
 "大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅",
 "豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞",
 "烤香腸","車(chē)輪餅","珍珠奶茶","鹹酥雞","大熱狗",
 "炸雞排","山豬肉","花生冰","剉冰","水果冰",
 "包心粉圓","排骨酥","沙茶魷魚(yú)","章魚(yú)燒","度小月",
 "aaa","abc","bbb","bcd","123"
 };
}

 

執行的結果如下:
listview2
如果不要用android內附的simple_list_item_2,改用自己定義的樣式,要怎麼作呢?像上面的範例,再加上一個(gè)評分的字串在地點(diǎn)的旁邊。首先先製作一個(gè)給List的項目用的layout,如下的xml,取名為mylistview1.xml。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="match_parent" 
 android:layout_width="fill_parent" 
 android:orientation="vertical">
 <TextView android:text="TextView" 
 android:id="@+id/textView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginLeft="6dip"
 android:layout_marginTop="6dip" 
 android:textAppearance="?android:attr/textAppearanceLarge">
 </TextView>
 <LinearLayout android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:id="@+id/linearLayout1" 
 android:orientation="horizontal">
 <TextView android:id="@+id/textView2" 
 android:text="TextView" 
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceSmall">
 </TextView>
 <TextView android:text="TextView" android:id="@+id/textView3" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceSmall" 
 android:layout_marginLeft="6dip">
 </TextView>
 </LinearLayout>
</LinearLayout>
 

 

接下來(lái)要改一下程式碼,改用自己定義的item layout,利用R.java裡面的定義就行了(下面程式碼省略了rating這個(gè)sting array的定義,記得加上):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//把資料加入ArrayList中
 for(int i=0; i<mPlaces.length; i++){
 HashMap<String,String> item = new HashMap<String,String>();
 item.put( "food", mFoods[i]);
 item.put( "place","地點(diǎn):"+mPlaces[i] );
 item.put("rating", "評分:"+mRatings[i]+" 星");
 list.add( item );
 }
 
 //新增SimpleAdapter
 adapter = new SimpleAdapter( 
 this, 
 list,
 R.layout.mylistview1, 
 new String[] { "food","place","rating" },
 new int[] { R.id.textView1, R.id.textView2, R.id.textView3 } );

 

 

執行的結果如下:

listview3

下一步,加上圖 片吧…沒(méi)圖片很難有真相,圖片需要先放到res/drawable-xxxx目錄中(這裡只有放到res/drawable-hdpi中),抓取圖片用R.drawable.pic,不過(guò)因為HashMap的value部份需要用圖片(R.drawable.pic),是一個(gè)int的型態(tài),所以HashMap的value部份需要改為Object,才能容得下int和string的類(lèi)型。
先將用於list的項目用的layout改一下,加上圖片在標題的左邊:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="match_parent" 
 android:layout_width="fill_parent" 
 android:orientation="vertical">
 <LinearLayout android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:id="@+id/linearLayout1" >
 <ImageView android:layout_height="wrap_content"
 android:id="@+id/imageView1" 
 android:layout_width="wrap_content" 
 android:src="@drawable/icon"></ImageView>
 <TextView android:text="TextView" 
 android:id="@+id/textView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginLeft="6dip"
 android:layout_marginTop="6dip" 
 android:textAppearance="?android:attr/textAppearanceLarge">
 </TextView>
 </LinearLayout>
 <LinearLayout android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:id="@+id/linearLayout1" 
 android:orientation="horizontal">
 <TextView android:id="@+id/textView2" 
 android:text="TextView" 
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceSmall">
 </TextView>
 <TextView android:text="TextView" android:id="@+id/textView3" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceSmall" 
 android:layout_marginLeft="6dip">
 </TextView>
 </LinearLayout>
</LinearLayout>
 

 

再來(lái)要改一下程式碼,加上圖片和改Hashmap的value型態(tài)為Object(部份程式碼):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
 private SimpleAdapter adapter;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 //把資料加入ArrayList中
 for(int i=0; i<mPlaces.length; i++){
 HashMap<String,Object> item = new HashMap<String,Object>();
 item.put("pic", mPics[i]);
 item.put( "food", mFoods[i]);
 item.put( "place","地點(diǎn):"+mPlaces[i] );
 item.put("rating", "評分:"+mRatings[i]+" 星");
 list.add( item );
 }
 
 //新增SimpleAdapter
 adapter = new SimpleAdapter( 
 this, 
 list,
 R.layout.mylistview2,
 new String[] { "pic","food","place","rating" },
 new int[] { R.id.imageView1, R.id.textView1, R.id.textView2, R.id.textView3 } );
 
 //ListActivity設定adapter
 setListAdapter( adapter );
 
 //啟用按鍵過(guò)濾功能,這兩行都會(huì )進(jìn)行過(guò)濾
 getListView().setTextFilterEnabled(true);
 }
 private static final int[] mPics=new int[]{
 R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5,
 R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5,
 R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5,
 R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5,
 R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5,
 R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5
 };
...

 

 
執行的結果如下:
listview4
寫(xiě)到這裡寫(xiě)得篇幅有點(diǎn)多了,這只是把一些基本的listview顯示資料的方式記錄下來(lái)而已,還沒(méi)真正開(kāi)始進(jìn)行操作。
以上圖片和文字僅供參考。
接下來(lái)的部份還在另一篇listview的文章中繼續來(lái)研究。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Android 設置頁(yè)面UI設計
Android 仿QQ主頁(yè)面的實(shí)
自定義PopupWindow動(dòng)畫(huà)效果
如何在android中制作角半徑
Android實(shí)現給TableLayou繪制邊框的方法
Creating a custom CursorAdapter for ListView ...
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久