问题
In my app I have a listview, which when you click on an item i want a dialogfragment to appear with details in it. The listview is populated using a custom cursoradapter and each row is a view extended from a relativelayout.
My thought process is that i would have some kind of id value in the custom view and when selected, the id is used in a new db query to populate the dialogfragment. However, I don't want the id on view in the row, I want it hidden.
I'm thinking that i create a custom view class, extended from relativelayout, and in the view is a field that will hold the id as well as the two textviews.
My main problem is working out how to use my custom view inside the adapter. Every example i have found, inflates an xml layout inside newview, but my xml layout will not include the id field.
Can I just create a new object for my view, inside newview rather than inflating an xml?
Or am I missing something obvious?
Thanks
回答1:
When you are working with CursorAdapter
in onItemClick(AdapterView<?> parent, View view, int position, long id)
implementation of AdapterView.OnItemClickListener
you can get:
- the
_id
column from the underlying Cursor - simply, it is thelong id
parameter of this function any column visible or not in your item view but existing in the Cursor by casting the item at position to the Cursor like:
Cursor c = (Cursor)parent.getItemAtPosition(position);
Now
c
is pointing to the selected row so you are can get any column from it as simple asvar value = c.getInt/String/etc(c.getColumnIndex("columnname"));
回答2:
Seems like you might be able to use a tag and that would be simpler.
view.setTag("myId");
// ...
String id = (String) view.getTag();
来源:https://stackoverflow.com/questions/22237915/android-hidden-field-in-listview-using-custom-cursoradapter