How to make the "adapter.getItemPosition" get only part of the String and hide the other party at the time of displaying the ListView?

1

I have a listView that returns two strings to one each row. The code and the name of a country, I would not like to present the code but I need it for when the user clicks on the line to get the code and search the bank. My line is like "345 Brazil", because I need the code I needed to get only it with adapter.getItem(position) . Is there any way to do this? Thank you in advance!

    
asked by anonymous 29.09.2015 / 15:52

3 answers

2

I suppose your adapter is of type ArrayAdapter<String> and hence its difficulty.
My suggestion is to declare a class named Country and declare the Adapter as ArrayList<Pais> .

Begin by declaring the Parent class:

public class Pais{
    String nome;
    String codigo;

    public Pais(String nome, String codigo){

        this.nome = nome;
        this.codigo = codigo;
    }

    public String getNome(){

        return nome;
    }

    public String getCodigo(){

        return codigo;
    }
}

Declare the array , which is used by the Adapter as ArrayList<Pais> and pass it to ArrayAdapter<Pais> .

When you need the name of the country do: getItem(position).getNome();
When you need the country code do: getItem(position).getCodigo();

    
29.09.2015 / 16:29
2

You can save the whole object to your Adapter , in your object you implement toString which is the method that Adapter will use to show in ListView , and when you need to get the code for example you have the object saved with all its attributes.

    
29.09.2015 / 15:57
0

I solved the problem guys, it was my gambiarra but it worked, now I only get the number of the string adapter.getItem(position).replaceAll("[^0-9]*", ""); But how could I hide this number? Display only the name of the country?

    
29.09.2015 / 15:56