2

I am trying to get the details for a clicked item in a ListView by querying the SQLite database. I am passing the long id value from onItemClick as the primary key. However, the long id does not match with the actual primary key in the Table. I found out the actual primary key by querying for the primary keys of all the rows in the Table.

I have the following questions: 1. Am I right in assuming that the long id parameter in onItemClick(AdapterView parent, View view, int position, long id) carries the primary key of the item in the database table?

  1. If not, how should I get the primary key?

I have appended my code below:

list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

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

            //Display Client Details
            Intent displayclient = new Intent(view.getContext(), display_clientdetails.class);
            Integer db_id = (int) id;

            String TAG = "client_list";
            Log.d(TAG,"Position is" +position +" ID is " +db_id);

            displayclient.putExtra("client_id",db_id);
            view.getContext().startActivity(displayclient);

        }

});

user2348956
  • 71
  • 2
  • 7

2 Answers2

3

No, you are not right. If you are using a custom adapter, you should override getItemId(int position) in order to control and return whatever are you considering your id.

I usually use something like this, but it depends of the type of your primary key. You also might have to override getItem(int position).

@Override
public long getItemId(int position) {
   return getItem(position).getId();
}
Jofre Mateu
  • 2,310
  • 13
  • 25
  • It worked for me after I created a custom adapter and wrote an override for the getItemId method. However, I havent understood the reason why it worked. It will be great if you could share the reason why getItemId doesnt return the primary key without being over-ridden. Thanks. – user2348956 Mar 02 '15 at 19:31
  • That's because the base function doesn't know what are you using as id. It would be nice if it worked out of the box, but considering how many diferent and custom objects you could pas to an adapter, it seems complicated. The default function returns the position parameter, as you can see here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ArrayAdapter.java – Jofre Mateu Mar 05 '15 at 15:02
0

I have the following questions: 1. Am I right in assuming that the long id parameter in onItemClick(AdapterView parent, View view, int position, long id) carries the primary key of the item in the database table?

yes you are wrong. The id, provided in onItemClick, is the result of what your adapter's getItemId returns.

Blackbelt
  • 148,780
  • 26
  • 271
  • 285
  • It worked for me after I created a custom adapter and wrote an override for the getItemId method. However, I havent understood the reason why it worked. It will be great if you could share the reason why getItemId doesnt return the primary key without being over-ridden. Thanks – user2348956 Mar 04 '15 at 18:50
  • what adapter were you using before switching to the custom one? – Blackbelt Mar 04 '15 at 18:51
  • ArrayAdapter's getItemId returns position – Blackbelt Mar 06 '15 at 14:54