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

Asked

Viewed 125 times

1

I have a listview where I list people’s names and when I click on a person I use the adapter.getitem(position) who takes the person’s name, however, I needed to get the person’s id 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, but present only the name in the listview and get the id when click. there are similar names, so I need the id.

BS: I’m currently doing something that I don’t know if it’s gambiarra or smart. list the names and when I click on the name uses the option to catch the id. For now it is working but is not a good practice.

  • How you are populating the Adapter of listview?

  • I’m using an Arrayadapter, I get a vector with the names of the bank and put in it.

  • Is Pessoa an entity (class) with id and name? Are you passing an Array of People to your listview? Add more information, not knowing what I questioned becomes difficult to help.

2 answers

1

When making the query to get the names and popular the listview, create a Arraylist and populate it with names ids.

In the Onclick event of the listview, you can return the id of the name clicked thus:

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);

  }
});

0

Hello

You can create a Customadapter for your Listview using a Basedapter. With it created, you can add each Person entity and at the click of listview recover only the person ID. Here has an example of how to create a custom with Basedapter.

I hope I’ve helped, any questions leave a comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.