从Android的SQLite数据库填充一个ListView [英] Populating a ListView from a SQLite database in Android

查看:132
本文介绍了从Android的SQLite数据库填充一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充一个ListView在一个单独的类从另一个类举行了SQLite数据库中获取的数据。什么是这样做的最简单的方法是什么?

I am trying to populate a ListView in a separate class with data taken from a SQLite database held in another class. What would be the easiest way to do so?

感谢

推荐答案

返回游标,例如:的GetList()从其他类到当前的ListView类

Return a Cursor eg: getList() from your other class to the current ListView class.

objItem = new Contacts(this);

this.cur = objItem.getList();
this.startManagingCursor(this.cur);

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur,
        new String[] { ContactsContract.Contacts.DISPLAY_NAME}, new int[] { android.R.id.text1});

setListAdapter(adapter);

在课堂上你有一个方法返回游标

in class you have a method returning Cursor

public Cursor getList() {
        // Get the base URI for the People table in the Contacts content
        // provider.
        Uri contacts = ContactsContract.Contacts.CONTENT_URI;
        // Make the query.
        ContentResolver cr = ctx.getContentResolver();
        // Form an array specifying which columns to return.
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };

        Cursor managedCursor = cr.query(contacts, projection, null, null,
                ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC");
        return managedCursor;
    }

这篇关于从Android的SQLite数据库填充一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆