一、SimpleAdapter
SimpleAdapter是一個簡單的適配器,可以將靜態數據映射到XML文件中定義 好的視圖。你可以指定數據支持的列表如ArrayList組成的Map。在ArrayList中 的每個條目對應List中的一行。Maps包含每行數據。你可以指定一個定義了被用 於顯示行的視圖XML文件,通過關鍵字映射到指定的視圖。
構造函數
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
參數
context 關聯SimpleAdapter運行着的視圖的上下文。
data 一個Map的列表。在列表中的每個條目對應列表中的一行,應該包含所 有在from中指定的條目
resource 一個定義列表項目的視圖佈局的資源唯一標識。佈局文件將至少應 包含哪些在to中定義了的名稱。
from 一個將被添加到Map上關聯每一個項目的列名稱的列表
to 應該在參數from顯示列的視圖。這些應該全是TextView。在列表中最初的 N視圖是從參數from中最初的N列獲取的值。
一個SimlpleAdapter是這樣工作的。假設將SimpleAdapter用於ListView。那 麼ListView的每一個列表項就是 resource參數值指定的佈局。而data參數就是 要加載到ListView中的數據。我們先看每一個列表項,假設列表項所對應的佈局 文件中包含了兩個組件:TextView和EditText,id分別爲textview和edittext。 那麼在加載列表項時,需要通過組件的id和data參數中 List元素中的Map對象對 應。因此,from參數Map對象的key,而to表示組件的id,例如,本例中的參數值 爲from = new String[]{"userId", "userName"},to = new int[]{R.id.userId,R.id.userName}。意思就是將Map對象中key爲userId 的value綁定到 R.id.userId上,userName也類似。現在來看data參數,一個 ListView由多個列表項組成。每一個列表項由一個Map對象提供數據,而多個列 表項則由List對象提供多個 Map對象。
二、ListView
在android開發中ListView是比較常用的組件,它以列表的形式展示具體內容 ,並且能夠根據數據的長度自適應顯示。
列表的顯示需要三個元素:
1.ListVeiw 用來展示列表的View。
2.適配器 用來把數據映射到ListView上的中介。
3.數據 具體的將被映射的字符串,圖片,或者基本組件。
根據列表的適配器類型,列表分爲三種,ArrayAdapter,SimpleAdapter和 SimpleCursorAdapter。其中以ArrayAdapter最爲簡單,只能展示一行字。 SimpleAdapter有最好的擴充性,可以自定義出各種效果。SimpleCursorAdapter 可以認爲是SimpleAdapter對數據庫的簡單結合,可以方面的把數據庫的內容以 列表的形式展示出來。SimpleAdapter繼承自AdapterView。我們可以通過 setOnItemClickListener()方法給ListView添加監聽器,當用戶點擊某一個列表 項中執行相應的操作。在監聽器中需要複寫public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)方法。
如果需要訪問與被選項相關的數據,執行程序可以調用getItemAtPosition (position)。
參數
parent 發生點擊動作的AdapterView。
view 在AdapterView中被點擊的視圖(它是由adapter提供的一個視圖)。
position 視圖在adapter中的位置。
id 被點擊元素的行id。
示例:我們一SimpleAdapter爲例說明ListView的用法。
Android_ListView.java
01.package com.idea.org; 02. 03.import java.util.ArrayList; 04.import java.util.HashMap; 05. 06.import android.app.Activity; 07.import android.os.Bundle; 08.import android.view.View; 09.import android.widget.AdapterView; 10.import android.widget.AdapterView.OnItemClickListener; 11.import android.widget.ListView; 12.import android.widget.SimpleAdapter; 13.import android.widget.Toast; 14. 15.public class Android_ListView extends Activity { 16. /** Called when the activity is first created. */ 17. @Override 18. public void onCreate(Bundle savedInstanceState) { 19. super.onCreate(savedInstanceState); 20. setContentView(R.layout.main); 21. ArrayList<HashMap<String,String>>list =new ArrayList<HashMap<String,String>>(); 22. HashMap<String,String> map1=new HashMap<String,String>(); 23. HashMap<String,String> map2=new HashMap<String,String>(); 24. HashMap<String,String> map3=new HashMap<String,String>(); 25. ListView listView=(ListView)findViewById(R.id.listView); 26. map1.put("userId", "100001"); 27. map1.put("userName", "用戶一"); 28. list.add(map1); 29. map2.put("userId", "100002"); 30. map2.put("userName", "用戶二"); 31. list.add(map2); 32. map3.put("userId", "100003"); 33. map3.put("userName", "用戶三"); 34. list.add(map3); 35. //定義一個SimpleAdapter,每一個行有兩個TextView,分別顯示userId和userName 36. SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.user, 37. new String[]{"userId","userName"},new int[]{R.id.userId,R.id.userName}); 38. //爲ListView添加適配器 39. listView.setAdapter(simpleAdapter);//設置listView背後的數據爲simpleAdapter。 40. /*爲listView添加單擊監聽器,需要import android.widget.AdapterView.OnItemClickListener;語句 41. * 當點擊某一個列表項時,用Toast顯示這個列表項中的文字內容 42. */ 43. listView.setOnItemClickListener(new OnItemClickListener() { 44. 45. @Override 46. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 47. long arg3) { 48. ListView listView=(ListView)arg0; 49. Toast.makeText(Android_ListView.this, listView.getItemAtPosition(arg2).toString(), 50. Toast.LENGTH_SHORT).show(); 51. } 52. }); 53. } 54.}
main.xml
01.<?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. android:orientation="vertical" 04. android:layout_width="fill_parent" 05. android:layout_height="wrap_content" 06. > 07. <!--添加一個ListView控件 --> 08. <ListView 09. android:id="@+id/listView" 10. android:layout_width="fill_parent" 11. android:layout_height="wrap_content" 12. /> 13.</LinearLayout>
SimpleAdapter所用的佈局文件user.xml
01.<?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. android:orientation="horizontal" 04. android:layout_width="fill_parent" 05. android:layout_height="fill_parent" 06. android:paddingRight="10dip" 07. android:paddingLeft="10dip" 08. android:paddingTop="1dip" 09. android:paddingBottom="1dip" 10. > 11. <!--ListView的每一項都有兩個TextView,分別顯示userId和userName --> 12. <TextView 13. android:id="@+id/userId" 14. android:layout_width="fill_parent" 15. android:layout_height="wrap_content" 16. android:textSize="20pt" 17. android:layout_weight="1" 18. /> 19. <TextView 20. android:id="@+id/userName" 21. android:layout_width="fill_parent" 22. android:layout_height="wrap_content" 23. android:textSize="20pt" 24. android:layout_weight="1" 25. /> 26.</LinearLayout>
運行效果
奇怪的是ListView無法充滿Android3.0模擬器的整個屏幕,不知道是由於 Android3.0版本的問題,還是其它的什麼原因。
來源:http://blog.csdn.net/ITCEOjingying/archive/2011/04/11/6315060.aspx