使用 SimpleCusorAdapter 填充 Android 的 ListView

发布于 2024-09-05 23:48:41 字数 1241 浏览 2 评论 0原文

我试图将 SQLite 数据库中的多条数据获取到 ListView 中,但它似乎不起作用。

我正在使用developer.android.com的记事本示例中的代码,它适用于1条数据,但不适用于2条数据。

我试图通过游标从数据库中获取每个数据库条目的标题和正文一个视图,我认为我的问题在于 xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
</LinearLayout>

这是我尝试绑定值的代码:

mNotesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(mNotesCursor);
String[] from = new String[]{NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY};
int[] to = new int[]{R.id.text1, R.id.text2};

SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    setListAdapter(notes);

如果我只将 KEY_TITLE 放入只有一个 TextView 的 .xml 中(就像在记事本教程中一样),那么就可以了,但是如果我尝试使用上面定义的 xml 运行它,它会强制关闭。

有什么想法吗?

感谢您抽出时间,

InfinitiFizz

I'm trying to get multiple pieces of data from an SQLite database into a ListView but it doesn't seem to be working.

I'm using the code from developer.android.com's Notepad example and it works fine for 1 piece of data but not 2.

I'm trying to get the title and body of each database entry from the database, through a cursor and into a view, I think my problem is with the xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
</LinearLayout>

Here is my code for trying to bind the values:

mNotesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(mNotesCursor);
String[] from = new String[]{NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY};
int[] to = new int[]{R.id.text1, R.id.text2};

SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    setListAdapter(notes);

If I am only getting KEY_TITLE into an .xml with only one TextView (like in the Notepad tutorial) then it is fine, but if I try and run it with the xml defined above, it force closes.

Any ideas why?

Thanks for your time,

InfinitiFizz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2024-09-12 23:48:41

如果您创建自定义适配器而不是使用“SimpleCursorAdapter”,那就更好了。

基本上,您需要做的是创建一个从“CursorAdapter”派生的新类,然后实现“newView”和“bindView”方法。
在这两种方法中,您都可以将光标指向正确的位置,以便您可以直接访问内容并在视图和数据之间进行映射。

It would be better if you create your custom adapter instead of using the "SimpleCursorAdapter".

basically, what you need to do is to create a new class that derives from "CursorAdapter" and then implement the "newView" and "bindView" methods.
in both of these methods you get the cursor pointed to the correct position so you can access the contents directly and map between your view and the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文