以下解决
请参阅下面的解决方案
我有一个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);";
--- --- UPDATE
我在我的表格中添加了_id
列,现在看来我_id
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);
}
});
答案 0 :(得分:1)
对于基于Cursor
的适配器,您获得id
回调中行的onItemClick
,是id
参数。你应该使用它。
答案 1 :(得分:1)
<强>解决强>
在我的表中添加了_ id 字段,然后将 onItemClick 提供的 long id 字段传递给我的方法 openDigitalBusinessCardActivity(id ); 。将 id 传递给 openDigitalBusinessCardActivity(id); 调用的意图。然后在新活动中使用 id 来查询数据库。
我的表:
private static final String DATABASE_CREATE =
"create table " + TABLE_SCORECARD + "(" + KEY_ROWID +" integer primary key autoincrement, "
+ COLUMN_MERCHANTID + " Integer, "
+ COLUMN_MERCHANTNAME + " text not null, "
+ COLUMN_MERCHANTTAGLINE + " text not null, "
+ COLUMN_CURRENTSCORE + " Integer, "
+ COLUMN_FREEBIEPOINT + " Integer, "
+ COLUMN_CAMPAIGNID + " Integer, "
+ COLUMN_LISTPOSITION + " Integer);";
在我的ListActivity中:
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + id, Toast.LENGTH_LONG)
.show();
openDigitalBusinessCardActivity(id);
}
});