0

已解决

解决方法见下文


我有一个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列,现在看起来我的_idfrom查询onItemClick没有返回任何内容,因为我对cur.moveToFirst()返回的测试false。所以onItemClick 没有返回有效的表行号。

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);              
        }
      });
4

2 回答 2

1

Cursor基于适配器的情况下,您在回调中获得id的行是参数。你应该使用它。onItemClickid

于 2012-05-05T09:15:42.483 回答
1

解决了

将_id字段添加到我的表中,然后将onItemClick提供的长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);
    }
   });
于 2012-05-06T06:00:02.077 回答