Roardog
Roardog

Reputation: 139

Fetching data from database from ListView's onItemClickListener

SOLVED BELOW

See below for solution


I have a ListView with a custom adapter which extends CursorAdapter. When the user clicks on an item in the ListView I want to open a new Activity(launched from the openDigitalBusinessCardActivity method), with the new Activity based upon the particular item in the ListView that was clicked. The new Activity needs the field merchantID from the database so I need to access this and then pass to the new Activity in my Intent.

However, I cannot get working how to access the correct data in my database based upon the feedback of the ListView click listener.

Eclipse gives me Bad request for field slot errors. My code for my listener is below:

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

My database creation code is this:

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---

I have added an _id column to my table and now it looks like my query by the _id from onItemClick is not returning anything as my test on cur.moveToFirst() returns false. So onItemClick is not returning a valid table row number.

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

Upvotes: 0

Views: 3461

Answers (2)

Roardog
Roardog

Reputation: 139

SOLVED

Added _id field to my table and then passed the long id field provided by onItemClick to my method openDigitalBusinessCardActivity(id);. Passed id to the intent called by openDigitalBusinessCardActivity(id);. Then used id in the new activity to query the database.

My table:

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);";

In my 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);
    }
   });

Upvotes: 1

user
user

Reputation: 87064

In the case of Cursor based adapters you get the id of the row in the onItemClick callback, is the id parameter. You should use that.

Upvotes: 1

Related Questions