How to get the item id in an onItemClick handler
Asked Answered
C

4

5

I have a category table with two columns category_id and name. I have created a data helper class named CategoryDataHelper. I have a method named getCategoryCursor() of that helper class which fetches the id and the name from the category table and returns the cursor. Using that cursor, I have used SimpleCursorAdapter to display the list of categories. It is working fine.

public class Categories extends ListActivity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categoryDataHelper = new CategoryDataHelper(getApplicationContext());
        Cursor categoryCursor  = categoryDataHelper.getCategoryCursor();
        ListAdapter adapter = new SimpleCursorAdapter (
                this,  
                android.R.layout.simple_list_item_1,
                categoryCursor,                                              
                new String[] { CategoryDataHelper.NAME },           
                new int[] {android.R.id.text1});  

        // Bind to our new adapter.
        setListAdapter(adapter);

        list = getListView();
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Here I want the category_id  
            }
        });
    }    
}

Now I want to implement an OnItemClickListener and send an Intent with the category_id of the selected category. How can I get the id in the onItemClick() method?

Costanzia answered 5/4, 2011 at 14:6 Comment(3)
isn't id parameter of type long helping you??Fete
For getting contents of item selected, use Object o=lv.getItemAtPosition(position); The object o can be further used to get items.Pianoforte
What does setListAdapter do and why does it come before list = getListView()?Protestation
L
17

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.

Cursor cursor = ((SimpleCursorAdapter) adapterView).getCursor();
cursor.moveToPosition(position);
long categoryId = cursor.getLong(cursor.getColumnIndex(CategoryDataHelper.ID));

or use "category_id" or whatever the name of your column is in place of CategoryDataHelper.ID.

Latin answered 5/4, 2011 at 14:22 Comment(2)
adapterView is usually not an adapter. It's a view that uses adapter and therefore have method getAdapter(). So, in your example you should call adapterView.getAdapter() instead.Stinkweed
Is it applicable with SimpleAdapter?Houseclean
C
3

Thanks Zack, I could solve with your post...Excelent!!! ... I send a parameter from an activity to another so:

Intent myIntent = new Intent(Clientes.this, Edc.class);
Cursor cursor = (Cursor) adapter.getItem(position);
myIntent.putExtra("CLIENTE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(myIntent);

In the other activity (EDC)....i get the parameter so:

int _clienteId = getIntent().getIntExtra("CLIENTE_ID", 0);
Cloakroom answered 13/8, 2011 at 17:12 Comment(0)
D
1

How about in onItemclick:

categoryCursor.moveToPosition(position);

and then from the returned cursor get the ID from your helper?

Disinfection answered 5/4, 2011 at 14:13 Comment(0)
C
1

With the SimpleCursorAdapter, the onItemClick function passes in the databases id for the selected item. So, the solution is simply

long category_id = id
Conakry answered 20/1, 2016 at 1:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.