Skip to content Skip to sidebar Skip to footer

Simplecursoradapter And Viewbinder - Binding Data To Listview Items To Be Retrieved On Click

So I've got a ListView (using a ListActivity) that I'm populating from a SQLiteDatabase. I'm trying to attach the ID (PK) of the row to the view, so that onListItemClick of each l

Solution 1:

This depends on your implementation of R.layout.favorite. If you have this layout contains a parent view with child TextViews for e.g. the tag you set is for the TextViews while the View v received from the onListItemClick() is the parent View. You need to make sure that you receive the tag for the same view you set by using:

@OverrideprotectedvoidonListItemClick(ListView l, View v, int position, long id) {
    ObjectwordID= v.getChild(0).getTag();          
    Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();      
    }    

Solution 2:

You probably should get the cursor from the adapter. This way if your cursor gets replaced you are still are still getting a valid cursor.

@OverrideprotectedvoidonListItemClick(ListView l, View v, int position, long id) {
       Cursorcursor=  adapter.getCursor();
       cursor.moveToPosition(position);
       Stringid= cursor.getString(cursor.getColumnIndex("primary key field name in database");
       Toast.makeText(getBaseContext(), "ID=" + id, 1).show();
    } 

NOTE : your adapter must be declared as a SimpleCursorAdapter other wise you should downcast it.

Post a Comment for "Simplecursoradapter And Viewbinder - Binding Data To Listview Items To Be Retrieved On Click"