vascoFG
vascoFG

Reputation: 129

Android SimpleCursorAdapter ID from Database

I'm having somewhat of an issue in an application I'm developing. So, all my tables have _id fields for primary keys, and I use SimpleCursorAdapter to bind them to ListViews and Spinners. What I wanted to know is how can I make the ListView or Spinner selected item have the same ID as the corresponding row?

The strange thing is that this works with the ContextMenu, which I am using straight of the NotePad example:

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
info.id

This ID field IS the same as the RowID on the table, and I can delete items fine, but when I try something like this:

getListView().setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg) {
            Intent i = new Intent(getApplicationContext(),
                    FuelingList.class);
            long id = view.getId();

The ID field is some random rubberish. So my question is, in the first code bit, what Id is the AdapterContextMenuInfo getting and how can I retrieve it in other parts of my code?

Upvotes: 2

Views: 4141

Answers (3)

stuckless
stuckless

Reputation: 6545

Since you know you are using a SimpleCursorAdapter, then in your onItemClick method, you can call

public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
   String value = ((SimpleCursorAdapter) adapter.getAdapter()).getCursor().getString(COLUMN_INDEX);
}

Where COLUMN_INDEX in the column that you want to fetch from the currently select row in the cursor.

Alternately, calling

adapter.getAdapter().getItem(position)

and then casting that to a Cursor, works as well.

NOTE 1: The AdapterView is actually your ListView.

NOTE 2: Since you using a SimpleCursorAdapter then getItem(position) on the Adapter returns the Cursor positioned at the row your specifed

NOTE 3: When you have Cursor, you can fetch data by providing the column index (0 based) or by using cursor.getString(cursor.getColumnIndex("COLUMN_NAME"))

NOTE 4: As per Barak's comment the arg parameter is the value from the _ID field when using a CursorAdapter, if all you need to know is the row _ID, then just use the arg value.

Upvotes: 3

Barak
Barak

Reputation: 16393

Since you are using a SimpleCursorAdapter, the onItemClick is passing the database row id in to you..

        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)  

The long arg part is actually your row id from the database.

So your code should be:

getListView().setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> adapter, View view, 
            int position, long arg) { 
        Intent i = new Intent(getApplicationContext(), 
                FuelingList.class); 
        long id = arg; 

Upvotes: 3

SKT
SKT

Reputation: 1851

Can you store the ids of rows in an arraylist and take the id of the row from corresponding position of arraylist. This worked for me..

protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Intent intent=new Intent(this , Details.class);
    Bundle extras = new Bundle();
    extras.putLong("ID", IDList.get(position));
    intent.putExtras(extras);
    startActivityForResult(intent, SHOW_DETAILS);

}

You can use listview.setonitenclicklistener... Hope it helps

Upvotes: 0

Related Questions