已解决
解决方法见下文
我有一个ListView
带有自定义适配器的扩展CursorAdapter
。当用户点击ListView
我想打开一个新项目Activity
(从openDigitalBusinessCardActivity
方法启动)时,新Activity
项目基于ListView
被点击的特定项目。新Activity
的需要数据库中的字段merchantID
,所以我需要访问它,然后传递给Activity
我的Intent
.
但是,我无法根据ListView
点击侦听器的反馈来了解如何访问数据库中的正确数据。
Eclipse
给我Bad request for field slot
错误。我的听众代码如下:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Click registered!",
Toast.LENGTH_SHORT).show();
db = myDbHelper.getReadableDatabase();
//Cursor cur = (Cursor) adapter.getCursor();
//cur.moveToPosition(position);
Cursor cur = (Cursor) parent.getItemAtPosition(position);
//Cursor cur = (Cursor)((MyCustomCursorAdapter) list.getAdapter().getItem(position));
int merchantID = cur.getInt(cur.getColumnIndex("merchantID")); //Get the merchantID
db.close();
openDigitalBusinessCardActivity(merchantID);
}
});
我的数据库创建代码是这样的:
private static final String DATABASE_CREATE =
"create table " + TABLE_SCORECARD + "("
+ COLUMN_MERCHANTID + " integer primary key, "
+ COLUMN_MERCHANTNAME + " text not null, "
+ COLUMN_MERCHANTTAGLINE + " text not null, "
+ COLUMN_CURRENTSCORE + " Integer, "
+ COLUMN_FREEBIEPOINT + " Integer, "
+ COLUMN_CAMPAIGNID + " Integer, "
+ COLUMN_LISTPOSITION + " Integer);";
- -更新 - -
我在表中添加了一个_id
列,现在看起来我的_id
from查询onItemClick
没有返回任何内容,因为我对cur.moveToFirst()
返回的测试false
。所以onItemClic
k 没有返回有效的表行号。
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Click registered!",
Toast.LENGTH_SHORT).show();
db = myDbHelper.getReadableDatabase();
//Cursor cur = (Cursor) adapter.getCursor();
//cur.moveToPosition((int) id);
//Cursor cur = (Cursor) parent.getItemAtPosition((int) id);
//Cursor cur = (Cursor)((MyCustomCursorAdapter) list.getAdapter().getItem(position));
Cursor cur = db.rawQuery("select merchantID from scoreCard where _id = "+id,null);
if(cur.moveToFirst()) {
//int merchantID = cur.getInt(cur.getColumnIndex("merchantID")); //Get the merchantID
Toast.makeText(getApplicationContext(), "cur.moveToFirst returned true",
Toast.LENGTH_SHORT).show();
}
cur.close();
db.close();
// openDigitalBusinessCardActivity(merchantID);
}
});