Android SimpleCursorAdapter问题
我在 My DataBaseHelper 类中编写了此方法:
public Cursor fetchData(String tableName) {
return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName, null);
}
并在我的活动之一中编写了此代码:
try {
Cursor cursor = myDbHelper.fetchData("tableName");
String[] columns = {cursor.getColumnName(0), cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.layout1, R.id.layout2};
SimpleCursorAdapter ca = new SimpleCursorAdapter
(this.getBaseContext(),
android.R.id.list, cursor,columns , columnsLayouts);
lv.setAdapter(ca); //lv is my ListView with id="@android:id/list"
txt.setText("Done ! : "); //Process is Ok
} catch (Exception e){
txt.setText("Error"); //Error happens
}
代码编译正常。但是,当到达此行 lv.setAdapter(ca);
时,它会强制关闭。
有问题吗? 我做错了吗?
-
更新: 这是 logCat 错误(在我通过以下方式过滤它们之后: android.view
)
I wrote this method in My DataBaseHelper class:
public Cursor fetchData(String tableName) {
return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName, null);
}
And wrote this code inside one of my activities:
try {
Cursor cursor = myDbHelper.fetchData("tableName");
String[] columns = {cursor.getColumnName(0), cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.layout1, R.id.layout2};
SimpleCursorAdapter ca = new SimpleCursorAdapter
(this.getBaseContext(),
android.R.id.list, cursor,columns , columnsLayouts);
lv.setAdapter(ca); //lv is my ListView with id="@android:id/list"
txt.setText("Done ! : "); //Process is Ok
} catch (Exception e){
txt.setText("Error"); //Error happens
}
The code is compiling ok. However, it closed forcefully when reaching this line lv.setAdapter(ca);
.
Is there a problem?
did I do it the wrong way?
-
UPDATE:
this is the logCat errors (After I filter them by: android.view
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当尝试扩充列表视图中的项目时,Android 会崩溃。 构造函数的第二个参数是布局 -
您正在传递 android.R.id.list,这对我来说听起来像是您正在传递列表 id 而不是项目的 id。
Android crashes when trying to inflate the items in your list view. The second parameter of the constructor is layout -
You are passing android.R.id.list which sounds to me like you are passing the list id instead of item's id.