使用ListView顯示數據

  在Android中,ListView是用來顯示一個列表的控件。每一行列表都是一個獨立的元素。這種控件既可以方便的顯示從系統中其他應用讀取出來的數據,也可獨立的爲各行元素設置監聽器。

    根據API文檔中的說明,使用ListView顯示控件的基本流程如下:

    1. 將所要顯示的數據以ArrayList,Cursor或者Array形式封裝成爲對象。

       eg1:

 
 

  1. public class Activity01 extends ListActivity {  
  2.     /** Called when the activity is first created. */ 
  3.     @Override 
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
  8.         HashMap<String, String> map1 = new HashMap<String, String>();  
  9.         HashMap<String, String> map2 = new HashMap<String, String>();  
  10.         HashMap<String, String> map3 = new HashMap<String, String>();  
  11.         map1.put("user_name""zhangsan");  
  12.         map1.put("user_ip""192.168.0.1");  
  13.         map2.put("user_name""zhangsan");  
  14.         map2.put("user_ip""192.168.0.2");  
  15.         map3.put("user_name""wangwu");  
  16.         map3.put("user_ip""192.168.0.3");  
  17.         list.add(map1);  
  18.         list.add(map2);  
  19.         list.add(map3);  
  20.         SimpleAdapter listAdapter = new SimpleAdapter(this, list,  
  21.                 R.layout.user, new String[] { "user_name""user_ip" },  
  22.                 new int[] { R.id.user_name,R.id.user_ip});  
  23.         setListAdapter(listAdapter);  
  24.     } 
  ArrayList一般用顯示靜態數據,其中又包含了容器。每一個容器中又可包含多個數據,作爲ListView的一行數據。

 

     eg2.  

 
 

  1. ContentResolver cr=getContentResolver();  
  2.         Cursor cur=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnullnullnull); 
  

Cursor一般用於讀取其他應用程序的制定的數據。讀取完畢後,Cursor指向所得結果集的第一項數據。

     eg3.              

 
 

  1. private static final String[] bs = {"a","b","c","d"}; 
 
數組用於顯示簡單的信息,如在spinner中顯示。
 
 
 

    2. 將所得到的數據加入到各自類型對應的適配器中。

         2.1 ArrayList型

  使用SimpleAdapter作爲適配器

public ArrayAdapter (Context context, int resource, int textViewResourceId)

Since: API Level 1

Constructor

Parameters
context The current context.
resource The resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceId The id of the TextView within the layout resource to be populated

               eg.  

 
 

  1. SimpleAdapter listAdapter = new SimpleAdapter(this, list,  
  2.  
  3.                  R.layout.user, new String[] { "user_name""user_ip" },  
  4.  
  5.                 new int[] { R.id.user_name,R.id.user_ip});  
  6.  
  7.                 setListAdapter(listAdapter);  
  8.  
  9.    
  10.  

       2.2 ContentProvider型

            使用SimpleCursorAdapter作爲適配器

     

Since: API Level 1

public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

Since: API Level 1

Constructor.

Parameters
context The context where the ListView associated with this SimpleListItemFactory is running
layout resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
c The database cursor. Can be null if the cursor is not available yet.
from A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.

 

            eg. 

 
 

  1. ListAdapter adapter = new SimpleCursorAdapter(this,    
  2.  
  3.            // 定義List中每一行的顯示模板  
  4.  
  5.           // 表示每一行包含兩個數據項 ,使用了系統內置模板  
  6.  
  7.             android.R.layout.simple_list_item_2,   
  8.  
  9.             // 數據庫的Cursor對象  
  10.  
  11.             cur,  
  12.  
  13.            // 從數據庫的NAME和NUMBER兩列中取數據  
  14.  
  15.             new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER },  
  16.  
  17.             // 與NAME和NUMBER對應的Views,系統內置的ID  
  18.  
  19.              new int[] { android.R.id.text1, android.R.id.text2 });  
  20.  
 

     2.3 數組型

          使用ArrayAdapter

public ArrayAdapter (Context context, int resource, int textViewResourceId)

Since: API Level 1

Constructor

Parameters
context The current context.
resource The resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceId

The id of the TextView within the layout resource to be populated

 

 

    
 
   3.將適配器添加到ListVeiw中,使用setAdapter(adap)方法。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章