java - 从 Android 中的 SQLite 数据库填充 ListView

标签 java android sqlite listview

我正在尝试使用从另一个类中保存的 SQLite 数据库中获取的数据填充单独类中的 ListView。最简单的方法是什么?

谢谢

最佳答案

返回一个游标,例如:getList() 从您的其他类到当前的 ListView 类。

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);

在类中你有一个返回 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;
    }

关于java - 从 Android 中的 SQLite 数据库填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2515375/

相关文章:

java - 青金石 ItemStack ID

java - Android SQLite 行不更新

android - 在 Android 上从 HTTP Post 获取 JSON 到 Algolia 服务器

sqlite - 如何从列表的 future 获取元素/值

list - 如何使用第二个列表中的列表值外键 id 从列表中检索 Flutter Dart 列表值

java - 错误消息无法正确显示

java - 使用 Struts2 在创建博客条目时生成动态 URL

java - 使用 Semaphore 和 acquire(int) 的 Java 代码中的死锁

android - 如何在没有年份的情况下检索从 Google 通讯录同步的联系人事件?

java - 如何使用数据库纠正 Recyclerview 中的此错误