android - hidden field in listview, using custom cursoradapter

烈酒焚心 提交于 2019-12-24 23:31:55

问题


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:

  1. the _id column from the underlying Cursor - simply, it is the long id parameter of this function
  2. 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 as var 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!