android Listview2 笔记
生活随笔
收集整理的這篇文章主要介紹了
android Listview2 笔记
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ListView編程的一般步驟
1)在布局文件中聲明ListView控件
2)?使用一維或多維動(dòng)態(tài)數(shù)組保存ListView要顯示的數(shù)據(jù)?;
3)?構(gòu)建適配器Adapter,將數(shù)據(jù)與顯示數(shù)據(jù)的布局頁(yè)面綁定;?
4)通過(guò)setAdapter()方法把適配器設(shè)置給ListView
第一步:編寫(xiě)布局文件main.xml,添加三個(gè)Textview和listview實(shí)現(xiàn)整體布局。具體代碼如下
?
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文件的內(nèi)容,方便程序或者main.xml訪問(wèn),具體代碼如下
?
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">單項(xiàng)打勾</string>
6 <string name="tv2">RadioButton</string>
7 <string name="tv3">CheckBox</string>
8 </resources>
?
第三步:修改ListView01.java,添加listview的相關(guān)操作,具體代碼如下
?
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 //得到三個(gè)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來(lái)保存listview要顯示的數(shù)據(jù)
19 String[] data = new String[]
20 {"邵洋江加油","你會(huì)成功的"};
21 //構(gòu)建適配器Adapter,將數(shù)據(jù)與顯示數(shù)據(jù)的布局頁(yè)面綁定;
22 ArrayAdapter<String> lv1Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,data);
23 //通過(guò)setAdapter()方法把適配器設(shè)置給ListView
24 listview1.setAdapter(lv1Adapter);
25 //listview里的內(nèi)容設(shè)置為單選
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 }
?
具體效果如下:
?
如果還想深入了解,下面的鏈接不錯(cuò)
?
?
?
Android之Adapter用法總結(jié):http://kb.cnblogs.com/a/2328334/
轉(zhuǎn)載于:https://www.cnblogs.com/shaoyangjiang/archive/2012/02/24/2366417.html
總結(jié)
以上是生活随笔為你收集整理的android Listview2 笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Nhibernate代码自动生成工具[转
- 下一篇: C++面向对象笔记:构造、析构函数、成员