How to get other data instead of the String that was clicked on the ListView?

0

I have a list of countries of type ListView, and when I click in Brazil I get the String "Brazil" with the "getItem (position)" but instead I would like to get another data, in the case the "BR" that is in the database. Does anyone have any idea how to get it? Note: I am getting all the data from the bank and using queries to extract results. Here is the code I'm using:

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

    String stringPaises = adapter.getItem(position);
    Intent i = new Intent(this, Activity2.class);
    i.putExtra("stringPaises", stringPaises);
    startActivityForResult(i, 0);

}
    
asked by anonymous 22.07.2015 / 22:29

1 answer

2

Try the following, create the following class:

Pais{
   String nome;
   String regiao;

    //gets e sets aqui

   public String toString(){
       return nome;
   }
}

no adapter do:

//pega do banco um Pais[] paises;
new ArrayAdapter(context, layout, paises)

Instead of populating an array of strings, create an array of strings from the database, and instead of passing the string array to the adapter, pass that array of strings.

What happens, the Adapter will show on the screen what to return in toString () and getItem (position) will return the Country, just call the object.getRegiao () for example.

    
23.07.2015 / 17:06