使用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)方法。

 

 

 

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