How to make "Adapter.getItemPosition" take only part of the String and hide the other part when presenting Listview?

Asked

Viewed 97 times

1

I have a listview that returns two strings for 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 the line take the code and do a search in the bank. My line is like "345 Brazil", for needing the code I needed to catch only him with the adapter.getItem(position). Is there any way to do this? Thank you in advance!

3 answers

2

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

2


I suppose your Adapter be the type ArrayAdapter<String> and hence its difficulty.
My suggestion is that you declare a class called Parents and declare the Adapter as ArrayList<Pais>.

Start by declaring the class Parents :

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 Adapter, as ArrayList<Pais> and pass it on to ArrayAdapter<Pais>.

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

  • thanks, I think that’s just what I need!

0

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

Browser other questions tagged

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