小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android中Adapter之SimpleCursorAdapter使用

 Go_Ahead 2012-11-19
         此Adapter(SimpleCursorAdapter)是Android專門為了連接數(shù)據(jù)庫與視圖而產(chǎn)生的.此例中借助ListView體現(xiàn)SimpleCursorAdapter的用法。因為,ListView在android中是比較常用的視圖組件,不論是靜態(tài)還是動態(tài)都可以在其上顯示。當然,在實際的android項目中需要顯示的數(shù)據(jù)一般都是動態(tài)從數(shù)據(jù)庫表中獲取的。而SimpleCursorAdapter就是將從數(shù)據(jù)庫表中獲取的數(shù)據(jù)顯示到ListView的橋梁。
         在做的時候,可以將SimpleCursorAdapter與前面的SimpleCursorAdapter進行對比一下,你會發(fā)現(xiàn)二者的使用非常相似:
          1.在new xxxAdapter時候, 如下 ,紅色字體,
               SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                                                                                                  this,
                                                                                                  android.R.layout.simple_expandable_list_item_1,
                                                                                                  cursor,
                                                                                                  new String[]{People.NAME},
                                                                                                  new int[]{android.R.id.text1}
              );
              SimpleAdapter adapter = new SimpleAdapter(
                                                                                       this,
                                                                                       allInfos ,
                                                                                       R.layout.simple_adapter_view,
                                                                                       new String[]{"img","name","phone","region"},
                                                                                       new int[]{R.id.info_img,R.id.info_name,R.id.info_phone,R.id.info_region}
          )
 紅色字體部分,即二者的數(shù)據(jù)源類型不同,SimpleCursorAdapter 的數(shù)據(jù)源是一個從數(shù)據(jù)庫表中查詢出來的數(shù)據(jù)集合cursor;而SimpleAdapter的數(shù)據(jù)源allInfos list集合(當然也可以是數(shù)組等),可以看到一個是動態(tài)一個是靜態(tài)的數(shù)據(jù)。
 
這里使用的SimpleCursorAdapter只是一個簡單的例子,用的是系統(tǒng)表,當然你可以自己建立一個表應用在此例中,activity代碼:
public class SimpleCursorAdapterTest extends Activity{
 private ListView listView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 listView = new ListView(this);
                 Cursor myCursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);//是系統(tǒng)里的表
                 startManagingCursor(myCursor);
                 ListAdapter listAdapter = new SimpleCursorAdapter(
                                                                                                   this, //上下文
                                                                                                   android.R.layout.simple_expandable_list_item_1, //要顯示的listview的樣式
                                                                                                   myCursor, //數(shù)據(jù)源
                                                                                                   new String[]{People.NAME}, //所對應的字段
                                                                                                   new int[]{android.R.id.text1}   //上面的字段顯示在界面上的那個控件
                 );
                 listView.setAdapter(listAdapter);
                 setContentView(listView);
 }
}
       值得注意的一點,不要忘了在AndroidManifest.xml文件中聲明訪問系統(tǒng)表的權(quán)限:<uses-permission android:name="android.permission.READ_CONTACTS"/>
如果該系統(tǒng)表中還沒有數(shù)據(jù),此時,所顯示出來的ListView是空的,沒有數(shù)據(jù)的。

    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多