Tonious
Tonious

Reputation: 53

Getting the row ID of a listview from a Android SQlite database

I have created a listview which displays all the people in the database. But how do I get the row id of the one I select so I can pass that infomation to the next activity.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.load_game1);

    listContent = (ListView)findViewById(R.id.contentlist);

    mDbAdapter = new DbAdapter(this);
    mDbAdapter.open();
    Cursor cursor = mDbAdapter.fetchAll();
    startManagingCursor(cursor);
    String[] from = new String[]{DbAdapter.KEY_NAME, DbAdapter.KEY_ROWID};
    int[] to = new int[]{R.id.text};
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
    listContent.setAdapter(cursorAdapter);
    mDbAdapter.close();
    }

Which displays displays http://i.minus.com/iGEtGKGyF1Wgc.png

Now I want to click on a name get that row id from the database.

Upvotes: 0

Views: 3472

Answers (3)

Dan S
Dan S

Reputation: 9189

Use this API call: cursorAdapter.getItemId(positionInListView)

Upvotes: 2

Flo
Flo

Reputation: 27455

You have to register implement and add an OnItemClickListener to the list so you can react when an item was clicked. The item id will be passed into the onListItemClick() method of the OnItemClickListener.

Upvotes: 0

Sunny
Sunny

Reputation: 14808

Use setOnItemClickListener on your Listview then you will get the id of the selected item (item id start from 0 but Database _id column is start from 1 so you should send id+1, please confirm this).

And send the id using intents.

Upvotes: 1

Related Questions