android學習--視圖列表(ListView和ListActivity),androidlistview
說明:
視圖列表(ListView和ListActivity)與AutoComplete、Spinner類似,它們都需要一個供顯示的清單項目,可以需要藉助於內容Adapter提供顯示清單項目
建立ListView有兩種方式:
(1)直接使用ListView進行建立
(2)Activity繼承ListActivity
ListView的常用XML屬性
下面分別用兩種方式建立ListView
方式一:直接使用ListView進行建立
(1) main_activity.xml
下面布局兩個listView,一個基於數組,一個基於Adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/language" /> <TextView android:layout_width="fill_parent" android:layout_height="30dp"/> <ListView android:id="@+id/listView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:headerDividersEnabled="true" android:footerDividersEnabled="true" android:divider="@drawable/img02"/> </LinearLayout>
(2) strings.xml 存放listView1內容數組
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">ListView</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string-array name="language"> <item>漢語</item> <item>韓語</item> <item>英語</item> <item>日語</item> <item>葡語</item> <item>俄語</item> </string-array></resources>
(3) MainActivity.java
步驟:
1、擷取布局ListView
2、封裝顯示內容list或數組
3、構建適配器Adapter
4、給ListView添加適配器
package com.example.listview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {private ListView listView2 = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//擷取ListViewlistView2 = (ListView) findViewById(R.id.listView2);//定義數組String arr[] = new String[]{"中國","韓國","日本","美國","葡萄牙","俄羅斯"};//聲明適配器 //this 上下文 //android.R.layout.simple_list_item_checked 列表樣式 //arr 顯示內容 (數組或list集合) ArrayAdapter<String > arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, arr);//listView添加適配器listView2.setAdapter(arrayAdapter);listView2.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Log.i("listView", parent.getItemAtPosition(position).toString());}});}}
運行效果如下:
如果想要自訂欄表,清單項目顯示多個組件。我們可以用SimpleAdapter來自訂我們的列表。
(1) main_activity.xml
布局了ImageView,TextView 組件,用於顯示列表內容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/img" android:layout_width="50dp" android:layout_height="65dp"/> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:layout_marginTop="30dp" /> <TextView android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginLeft="150dp" /> </LinearLayout></LinearLayout>
(2)MainActivity.java 步驟與上面的一樣
package com.example.listviews;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {private ListView listView = null; //定義ListView組件@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//擷取listView組件listView = (ListView) findViewById(R.id.listView);//初始化基礎資料String name[] = new String[]{"張三","李四","王五","張飛"};String phone[] = new String[]{"14313426573","15908263541","18012345678","13423456789"};int img [] = new int[]{R.drawable.name01,R.drawable.name02,R.drawable.name03,R.drawable.name04};//封裝list集合資料List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();for(int i=0;i<name.length;i++){Map<String,Object> listItem = new HashMap<String, Object>();listItem.put("img", img[i]);listItem.put("name", name[i]);listItem.put("phone", phone[i]);list.add(listItem);}//配置SimpleAdapter適配器//this 上下文//R.layout.activity_main 布局檔案//new String[]{"img","name","phone"} 列表內容key//new int[]{R.id.img,R.id.name,R.id.phone} 用於顯示內容的布局組件SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.activity_main,new String[]{"img","name","phone"}, new int[]{R.id.img,R.id.name,R.id.phone});//添加適配器listView.setAdapter(simpleAdapter);}}
運行效果如下:
除了SimpleAdapter之外,我們還可以重寫BaseAdapter的getView方法作為列表的清單項目 。 有興趣的同學可以動手寫寫BaseAdapter
方式二:Activity繼承ListActivity
注意:
ListActivity的預設布局由一個位於螢幕中心的全屏列表構成。如果你不想使用預設的布局,可以在onCreate()方法中通過setContentView()方法設定你自己的布局。如果指定你自己定製的布局,你的布局中必須包含一個id為"@id/android:list"的ListView。 若你還指定了一個id為"@id/android:empty"的view,當ListView中沒有資料要顯示時,這個view就會被顯示,同時ListView會被隱藏
(1)main_activity.xml
布局了一個ListView和TextView,TextView的id為@id/android:empty,當ListView中沒有資料要顯示時,這個TextView就會被顯示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/android:list" android:layout_width="match_parent" android:layout_height="match_parent"/> <TextView android:id="@android:id/empty" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:text="No data"/> </LinearLayout>
(2) MainActivity.java
package com.example.listactivity;import android.app.ListActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends ListActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String arr[] = new String[]{"中國","韓國","日本","美國","葡萄牙","俄羅斯"};//設定適配器ArrayAdapter<String > arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, arr);//給ListActivity添加適配器setListAdapter(arrayAdapter);}}
如果ListView沒有內容,則顯示TextView。如下
方式一和方式二的效果完全一樣,不同的是,一個給ListView組件添加適配器,一個給ListActivity類添加適配器而已。
安卓ListActivity 的問題 :一個 ListActivity 中並未指定 ListView組件,怎產生了一個Listview?
自己重寫一個ListView的適配器就好了,一般情況下最好都自己寫適配器
wenku.baidu.com/...7.html
參考一下
Android ListActivity問題
在oncreate方法中設定setcontentview把布局載入進去,然後找到一個布局的id,例如線性布局的ll,
然後ll.addview(lv);把lv加進去就好了