SimpleCursorAdapter 和 ListView 问题

发布于 2024-10-31 15:55:53 字数 1209 浏览 8 评论 0原文

我在列表视图中显示数据库中的记录时遇到问题。这是我的代码

public class FirstTab extends ListActivity {    


private DatabaseHelper dbhelper;


@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);

    setContentView(R.layout.first);

    dbhelper = new DatabaseHelper(getApplicationContext());
    Cursor cursor = dbhelper.getAllNotes();
    startManagingCursor(cursor);


    String[] columns = new String[] {DatabaseHelper.colTitle , DatabaseHelper.colDate};

    int[] to = new int[] { android.R.id.text1, android.R.id.text2};

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);

    setListAdapter(mAdapter);
}
}

...
public Cursor getAllNotes()
 {
     SQLiteDatabase db=this.getReadableDatabase();

     return db.query(noteTable, new String [] {colTitle, colDesc, colDate}, null, null, null, null, null);

 }
...

如果您需要更多,这里是 repo https://github.com/grzegorz-l/ myHomeworks

当我启动我的应用程序时,它从一开始就崩溃(RuntimeException)。如果我在 onCreate 方法中注释最后 2 行,它会运行,但 ofc 不会显示任何内容。

预先感

谢格雷格

I have a problem displaying an records from my db in listview. This is my code

public class FirstTab extends ListActivity {    


private DatabaseHelper dbhelper;


@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);

    setContentView(R.layout.first);

    dbhelper = new DatabaseHelper(getApplicationContext());
    Cursor cursor = dbhelper.getAllNotes();
    startManagingCursor(cursor);


    String[] columns = new String[] {DatabaseHelper.colTitle , DatabaseHelper.colDate};

    int[] to = new int[] { android.R.id.text1, android.R.id.text2};

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);

    setListAdapter(mAdapter);
}
}

and

...
public Cursor getAllNotes()
 {
     SQLiteDatabase db=this.getReadableDatabase();

     return db.query(noteTable, new String [] {colTitle, colDesc, colDate}, null, null, null, null, null);

 }
...

if you need more , here is repo https://github.com/grzegorz-l/myHomeworks

When i start my app it crash at the beggining (RuntimeException). If I comment 2 last lines in onCreate method it run but ofc doesn't show anything.

Thanks in advance

Greg

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

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

发布评论

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

评论(2

对你而言 2024-11-07 15:55:53

创建 SimpleCursorAdapter 时不要传递 getApplicationContext()。请使用 this 代替,即 ListActivity 的上下文。

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);

Don't pass getApplicationContext() when creating SimpleCursorAdapter. Use this instead, i.e., the context of your ListActivity.

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
蓝海 2024-11-07 15:55:53

您的 FirstTab.java 文件正在扩展 ListActivity。这意味着它的布局文件必须包含一个 ListView,其 android:id 设置为:

android:id="@android:id/list"

另外,您的 FirstTab.java 的布局指向 note_entry.xml。它应该指向其中具有上述描述的 ListView 的布局,或者不由 ListActivity 扩展。

Your FirstTab.java file is extending a ListActivity. This means that it's layout file has to contain a ListView with android:id set to:

android:id="@android:id/list"

Also, your FirstTab.java has it's layout pointing to note_entry.xml. It should either point to a layout which has a ListView of above description in it or not be extended by a ListActivity.

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