您当前位置:郑州市吉聚机械设备有限公司 > 关于我们 > 正文

Android中Adapter之SimpleCursorAdapter行使

时间:2021-01-23  来源:未知   作者:admin

         此Adapter(SimpleCursorAdapter)是Android特意为了连接数据库与视图而产生的.此例中借助ListView表现SimpleCursorAdapter的用法。由于,ListView在android中是比较常用的视图组件,岂论是静态照样动态都能够在其上表现。自然,在实际的android项现在中必要表现的数据清淡都是动态从数据库外中获取的。而SimpleCursorAdapter就是将从数据库外中获取的数据表现到ListView的桥梁。         在做的时候,能够将SimpleCursorAdapter与前线的SimpleCursorAdapter进走对比一下,你会发现二者的行使特意相通:          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}           ) 红色字体片面,即二者的数据源类型迥异,SimpleCursorAdapter 的数据源是一个从数据库外中查询出来的数据荟萃cursor;而SimpleAdapter的数据源allInfos 是list荟萃(自然也能够是数组等),能够望到一个是动态一个是静态的数据。 这边行使的SimpleCursorAdapter只是一个浅易的例子,用的是编制外,自然你能够本身竖立一个外行使在此例中,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);//是编制里的外                 startManagingCursor(myCursor);                 ListAdapter listAdapter = new SimpleCursorAdapter(                                                                                                   this, //上下文                                                                                                   android.R.layout.simple_expandable_list_item_1, //要表现的listview的形式                                                                                                   myCursor, //数据源                                                                                                   new String[]{People.NAME}, //所对答的字段                                                                                                   new int[]{android.R.id.text1}   //上面的字段表现在界面上的谁人控件                 );                 listView.setAdapter(listAdapter);                 setContentView(listView); }}       值得仔细的一点,不要忘了在AndroidManifest.xml文件中声明访问编制外的权限:<uses-permission android:name="android.permission.READ_CONTACTS"/>倘若该编制外中还异国数据,此时,所表现出来的ListView是空的,异国数据的。

Powered by 郑州市吉聚机械设备有限公司 @2018 RSS地图 HTML地图