android Listview2 筆記

來源:互聯網
上載者:User

ListView編程的一般步驟

1)在布局檔案中聲明ListView控制項

2) 使用一維或多維動態數組儲存ListView要顯示的資料 ;

3) 構建適配器Adapter,將資料與顯示資料的布局頁面綁定; 

4)通過setAdapter()方法把適配器設定給ListView

第一步:編寫布局檔案main.xml,添加三個Textview和listview實現整體布局。具體代碼如下

 

View Code

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:text="@string/tv1"
10 android:background="#FFF"
11 android:textColor="#888"
12 android:gravity="center"/>
13 <ListView android:id="@+id/lvCheckedTextView"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"/>
16
17 <TextView android:layout_width="fill_parent"
18 android:layout_height="wrap_content"
19 android:text="@string/tv2"
20 android:background="#FFF"
21 android:textColor="#888"
22 android:gravity="center"/>
23 <ListView android:id="@+id/lvRadioButton"
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"/>
26
27 <TextView android:layout_width="fill_parent"
28 android:layout_height="wrap_content"
29 android:text="@string/tv3"
30 android:background="#FFF"
31 android:textColor="#888"
32 android:gravity="center"/>
33 <ListView android:id="@+id/lvCheckedButton"
34 android:layout_width="fill_parent"
35 android:layout_height="wrap_content"/>
36
37 </LinearLayout>

 

第二步:修改String.xml檔案的內容,方便程式或者main.xml訪問,具體代碼如下

 

View Code

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <string name="hello">Hello World, Listview02Activity!</string>
4 <string name="app_name">Listview02</string>
5 <string name="tv1">單項打勾</string>
6 <string name="tv2">RadioButton</string>
7 <string name="tv3">CheckBox</string>
8 </resources>

 

第三步:修改ListView01.java,添加listview的相關操作,具體代碼如下

 

View Code

 1 package cn.shaoyangjiang.com;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.widget.ArrayAdapter;
6 import android.widget.ListView;
7
8 public class Listview02Activity extends Activity {
9 /** Called when the activity is first created. */
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.main);
14 //得到三個listview
15 ListView listview1 = (ListView)findViewById(R.id.lvCheckedTextView);
16 ListView listview2 = (ListView)findViewById(R.id.lvRadioButton);
17 ListView listview3 = (ListView)findViewById(R.id.lvCheckedButton);
18 //用string來儲存listview要顯示的資料
19 String[] data = new String[]
20 {"邵洋江加油","你會成功的"};
21 //構建適配器Adapter,將資料與顯示資料的布局頁面綁定;
22 ArrayAdapter<String> lv1Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,data);
23 //通過setAdapter()方法把適配器設定給ListView
24 listview1.setAdapter(lv1Adapter);
25 //listview裡的內容設定為單選
26 listview1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
27
28 ArrayAdapter<String> lv2Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,data);
29 listview2.setAdapter(lv2Adapter);
30 listview2.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
31
32 ArrayAdapter<String> lv3Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,data);
33 listview3.setAdapter(lv3Adapter);
34 listview3.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
35 }
36 }

 

具體效果如下:



 

如果還想深入瞭解,下面的連結不錯

 

 

 

Android之Adapter用法總結:http://kb.cnblogs.com/a/2328334/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.