Android ApiDemos示例解析(169):Views->Lists->2. Cursor(People)

SimpleCursorAdapter 的構造函數如下

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

context: context對象,可以使用activity。
layout: layout 資源ID simple_gallery_item.xml
cursor: cursor對象,本例為通訊錄cursor.
from: 需要顯示的列名數組
to: 列對應的View 的id
[java]
// Get a cursor with all people  
Cursor c = getContentResolver().query(People.CONTENT_URI, 
 null, null, null, null); 
startManagingCursor(c); 
 
ListAdapter adapter = new SimpleCursorAdapter(this, 
 // Use a template that displays a text view  
 android.R.layout.simple_list_item_1, 
 // Give the cursor to the list adatper  
 c, 
 // Map the NAME column in the people database to…  
 new String[] {People.NAME} , 
 // The "text1" view defined in the XML template  
 new int[] {android.R.id.text1}); 
setListAdapter(adapter); 

// Get a cursor with all people
Cursor c = getContentResolver().query(People.CONTENT_URI,
 null, null, null, null);
startManagingCursor(c);

ListAdapter adapter = new SimpleCursorAdapter(this,
 // Use a template that displays a text view
 android.R.layout.simple_list_item_1,
 // Give the cursor to the list adatper
 c,
 // Map the NAME column in the people database to…
 new String[] {People.NAME} ,
 // The "text1" view defined in the XML template
 new int[] {android.R.id.text1});
setListAdapter(adapter);

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *