1

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

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

 }
...

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

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

提前致谢

格雷格

4

2 回答 2

2

getApplicationContext()创建 SimpleCursorAdapter 时不要通过。改为使用this,即您的 ListActivity 的上下文。

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
于 2011-04-08T21:04:04.743 回答
0

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

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

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

于 2011-04-09T02:10:31.997 回答