SimpleCursorAdapter 和 ListView 问题
我在列表视图中显示数据库中的记录时遇到问题。这是我的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建 SimpleCursorAdapter 时不要传递
getApplicationContext()
。请使用this
代替,即 ListActivity 的上下文。Don't pass
getApplicationContext()
when creating SimpleCursorAdapter. Usethis
instead, i.e., the context of your ListActivity.您的 FirstTab.java 文件正在扩展 ListActivity。这意味着它的布局文件必须包含一个 ListView,其 android:id 设置为:
另外,您的 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:
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.