android - SimpleCursorAdapter 和 ListView 问题

标签 android listview simplecursoradapter

我在 ListView 中显示来 self 的数据库的记录时遇到问题。这是我的代码

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 不显示任何内容。

提前致谢

格雷格

最佳答案

在创建 SimpleCursorAdapter 时不要传递 getApplicationContext()。请改用 this,即您的 ListActivity 的上下文。

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

关于android - SimpleCursorAdapter 和 ListView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600615/

相关文章:

java - Android VPN认证和加密

android - 如何更改在抽屉导航内的回收 View 中单击的项目的文本颜色和图像

c# - 使用 asp.net listview 删除 SweetAlert 确认对话框?

android - 强制关闭创建简单的游标适配器

android - 用户是否必须在升级/安装时授予 com.android.vending.BILLING 权限?

具有固定宽度列的 WPF TreeView

android - ListView 的 setOnClickListener 不起作用

java - notifyDataSetChanged() 在 ListFragment 中不起作用

android - SimpleCursorAdapter 内的按钮

android - 如何更改 CollapsingToolbarLayout 字体和大小?