ListView and hidden identifier. How is this possible?

I am developing an Android application and now I have implemented a ListView that shows a list of courses related to the database.

I would like to know how to include a hidden identifier with the name (which comes from db) so that after the user clicks on the elements, the application will switch to the relative representation of the selected courses.

And how can I maintain the identifier while navigating inside the course?

At the moment, my code just loads the name of the courses from db and is set as a list:

ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1); for (CourseRecord c : get()) result.add(c.getFullname()); lv.setAdapter(result); 

Obviously, I can do c.getid () as well, but I'm not where I need to put the identifier.

Thank you very much.

PS: Maybe someone has really good list browsing graphics?

+4
source share
3 answers

change the array adapter as follows.

 private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){ @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.row, null); } v.setTag(getMyIdForPosition(position)); return convertView; } }; 

and have a click handler to get the selected identifiers

  private OnItemClickListener itemClickedHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { String myId = (String)v.getTag(); doYourStuff(myId); } }; 

assign list to listener

 myList= (ListView)findViewById(R.id.history); myList.setOnItemClickListener(itemClickedHandler); 
+10
source

You can save the identifier in a hidden TextView. In the XML list item, add 'android: visibility = "gone" to the TextView. Similarly, in the click handler, you can read the identifier from the text field.

+2
source

You can also save the identifier using the setTag (Object object) method for presentation. Use the getTag () method to extract this identifier from this view.

+1
source

Source: https://habr.com/ru/post/1347818/


All Articles