Skip to content Skip to sidebar Skip to footer

Android - Onclick Handler For Dynamic Listview Data?

I have a ListView that is filled with a dynamic set of data each time the Activity is created. For simplicity's sake, lets say each ListView item is a employee's name. Under the ho

Solution 1:

Use the ArrayAdapter with a list Employees - assuming you have Employee-object.

publicclassMyActivityextendsListActivity {
   // list of employeesprivate List<Employee> employees = newArrayList<Employee>();

Then use the position parameter in OnItemClick to get the employee-object.

publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id){
    int employee_id = ((Employee) getListAdapter().getItem(position)).getId();
    // ...
 }

You can also extend the ArrayAdapter to tailor the list for employee-objects: How to use ArrayAdapter<myClass>

Solution 2:

You use an Adapter based on some set of data (for example, a List of Employee objects). When you set an onItemClickListener on your ListView, the onItemClick method gets an int parameter that is the position in the underlying data. Use that to pluck the Employee object out of your List, and there's the employee id.

Solution 3:

I would suggest doing what you stated above. Or, I don't really think it's nice solution, I also think there aint any, but you could add a label with no content (maxHeight and maxWdith are both 0, or invisible? never tried that) and set the text. I don't know how it is resource wise. But you won't have to save the list anymore, which saves some memory if you have a big list.

Post a Comment for "Android - Onclick Handler For Dynamic Listview Data?"