Is it possible to put Abels in Listview?

Asked

Viewed 101 times

1

I have several ListView's that I use for browsing, and put as content id's.

So, a list that should present country names, is showing the country id.

Is there any way to keep those id's list but display name for users?

I need to keep the ids to do the searches in the bank.

  • Put your code to get an idea of how you are doing, and yes it is possible to put the label, inside your Adapter you can do this.

3 answers

2


You will need a Adapter who inherits from BaseAdapter. This will enable you to Adapter accept other objects.

For this, you will need to create a template Pais which has as its attribute id and the nome of the country and move to your Adapter and finally define this Adapter for your ListView.

Try something like that:

Parents:

public class Pais {
    private final int id;
    private final String nome;

    public Pais(int id, String nome) {
        this.id = id;
        this.nome = nome;
    }

    public int getId() {
        return id;
    }

    public String getNome() {
        return nome;
    }
}

item_parents.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_nome"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Meuadapter:

public class MeuAdapter extends BaseAdapter {

    private final List<Pais> paises;
    private final LayoutInflater inflater;

    MeuAdapter(List<Pais> paises, Context context) {
        this.paises = paises;
        //LayoutInflater para inflar o seu Layout
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return paises.size();
    }

    @Override
    public Object getItem(int position) {
        return paises.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_pais, null);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();

        if (holder == null) {
            holder = new ViewHolder();
            holder.tvNome = (TextView) convertView.findViewById(R.id.tv_nome);
        }

        //Recupeando o seu pais de acordo com a posição
        Pais pais = paises.get(position);

        //Definindo o texto do item da sua linha
        holder.tvNome.setText(pais.getNome());

        convertView.setTag(holder);

        return convertView;
    }

    class ViewHolder {
        TextView tvNome;
    }
}

Mainactivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Pais> paises = new ArrayList<>();
        paises.add(new Pais(0, "Brasil"));
        paises.add(new Pais(1, "Argentina"));
        paises.add(new Pais(2, "Portugal"));

        MeuAdapter meuAdapter = new MeuAdapter(paises, this);

        ListView listView = (ListView) findViewById(R.id.sua_list_view);
        listView.setAdapter(meuAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Pais pais = (Pais) parent.getAdapter().getItem(position);

                //Recuperando o ID do país.
                Toast.makeText(MainActivity.this, String.valueOf(pais.getId()), Toast.LENGTH_SHORT).show();
            }
        });
    }    
}

0

Yes, for this case the solution is the Adapter. Most of the visual components are designed in the MVC standard (I think it is very well known in the web environment).

Well, the ListView has a model (which is an internal data set), this data set can be "injected" through a Adapter which is nothing more than the behavior to render the View.

Documentation

0

Without the code it gets a little complicated, but I imagine you’re filling in the ListView using a CursorAdapter, right? (It must be similar to what I did here )

Where appropriate, in the method bindView() is when you "arrow" the text, in which case, do not use the ID. Create a method to receive the ID and return the country name.

Browser other questions tagged

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