ListView详解2

ListView详解2,第1张

概述二、CursorAdapter通过游标获得数据后,在listview中显示结果。Cursorcursor=getContentResolver().query(People.CONTENT_URI,null,null,null,null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。startManagingCursor(cursor);我们将获得的Cursor对象

二、CursorAdapter

通过游标获得数据后,在ListvIEw中显示结果。

Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。

 startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步。

 SimpleCursorAdapter 构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件ID的int型数组。其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应ID的组件上。下面的代码,将name列的数据一次映射到布局文件的ID为text1的组件上。

注意:需要在AndroIDManifest.xml中如权限:<uses-permission androID:name="androID.permission.READ_CONTACTS"></uses-permission>

public class MainActivity extends Activity {     private ListVIEw ListVIEw;	@OverrIDe    public voID onCreate(Bundle savedInstanceState){		super.onCreate(savedInstanceState);		ListVIEw = new ListVIEw(this);		Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,				null, null, null);		startManagingCursor(cursor);		listadapter listadapter = new SimpleCursorAdapter(this,				androID.R.layout.simple_expandable_List_item_1, cursor,				new String[] { People.name }, new int[] { androID.R.ID.text1 });		ListVIEw.setAdapter(listadapter);		setContentVIEw(ListVIEw);    }}
*CursorAdapter 常与数据库查询配合使用,讲获得的数据结果显示在ListvIEw中。

如果将simplecursoradapter改成这样:

listadapter listadapter = new SimpleCursorAdapter(this,				androID.R.layout.simple_expandable_List_item_2, cursor,				new String[] { People.name, People.name }, new int[] { androID.R.ID.text1,androID.R.ID.text2 });


则会显示androID.R.layout.simple_expandable_List_item_2对应格式的List。



总结

以上是内存溢出为你收集整理的ListView详解2全部内容,希望文章能够帮你解决ListView详解2所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1006145.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存