How to receive two data in a listview but only present one and use the other in the click?

1

I have a listview where people name and when I click on a person I use the adapter.getitem(position) that takes the person's name, however, I needed to get the id of the person and not the name. Does anyone know how I can do this? Briefly I need to bring 2 database data, the name and the id, however only present the name in the listView and get the id when clicking. there are similar names, so I need the id.

obs: I'm currently doing something I do not know if it's gambiarra or it's smart. list the names and when I click on the name use the option to get the id. It's working right now but it's not good practice.

    
asked by anonymous 30.11.2015 / 19:48

2 answers

1

When you query to get the names and popular the listview, create a ArrayList and populate it with the ids of the names.

In the OnClick event of the listview, you can return the id of the name clicked like this:

seuListView.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {

   //retornando o id do nome clicado a partir do ArrayList já populado
   String id= arrayDeId.get(position);

  }
});
    
30.11.2015 / 21:45
0

Hello

You can create a CustomAdapter for your ListView using a BaseAdapter. With it created, you can add each Person entity and in the listview click retrieve only the person ID. Here has an example of how to create a custom with BaseAdapter.

I hope to have helped, any questions leave a comment.

    
01.12.2015 / 12:12