How to get the value of a particular item from the java android listview

Asked

Viewed 7,556 times

1

How I get the value of a particular item(Textview) from listview.

I’m trying something like this:

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

Object obj =a.getItemAtPosition(position);

String mensagem = "Cliente Selecionado: " + obj;

Toast.makeText(getApplicationContext(), mensagem, Toast.LENGTH_SHORT)
        .show();}

but returns the selected client and that’s not what I need. I remember that in my listview there are several Textview

1 answer

4


According to the documentation of AdapterView.OnItemClickListener, to View v passed by parameter is the View of the item clicked on ListView.

Soon to recover some View inside this item just use it so:

@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    // Usar o v
    TextView tv = (TextView) v;

    // Buscar alguma view dentro deste item
    TextView tv2 = v.findViewById(R.id.idDoTextView);
}

So if you used the Adapter correctly to fill in the data of the ListView, soon it would be good to access the data of the Adapter than the View of ListView, given that the findViewById is more expensive than accessing the item.

  • Exactly what I needed, worked, thanks @wakim!

Browser other questions tagged

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