从ListView的onItemClickListener中获取数据库中的数据

时间:2012-05-05 09:11:03

标签: android database sqlite android-listview

以下解决

请参阅下面的解决方案


我有一个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);              
        }
      });

2 个答案:

答案 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);
    }
   });