GuybrushThreepwood
GuybrushThreepwood

Reputation: 5616

Capturing Database ID in Onlclick Method

I have a list view which is populated from a SQL database. I am using the following onlick code which detects when a cell is chosen :

ListView yourList = (ListView) findViewById(android.R.id.list);

    yourList.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  long arg3) {
                        Log.e("onClick",""+arg2);                                                                  
                        }   
    });

Currently this code is telling me what row has been clicked on. What I want to capture however is a field from the database called _ID. The order that the items appear in my list view is not the same order they are in the database, as I have sorted them alphbetically in my app code.

Can anyone help ?

EDIT I understand I can use arg3 - however it is returning 0 for every row. How do I correct this ?

Upvotes: 1

Views: 76

Answers (1)

Greyson
Greyson

Reputation: 3678

The id is included in the callback. If you look at the documentation of the OnItemClickListener, the method signature is:

 public abstract void onItemClick (
        AdapterView<?> parent, View view, int position, long id)

Notice 'id' field in your example is 'arg3'. This is the '_id' field from the database that you're looking for.

Upvotes: 1

Related Questions