Romain Pellerin
Romain Pellerin

Reputation: 2470

Retrieve view inside a row of a ListView Android

I have a listview with 4 identical rows. Inside those rows, I have a RelativeLayout which contains a TextView (id : R.id.notif). In my Activity, I use my own ArrayAdapter.

I would like to be able to modify the text of the third row. I tried this but it isn't working.

((TextView)listview.getAdapter().getView(2, null, listview).findViewById(R.id.notif)).setText("50");

Thank you.

Upvotes: 0

Views: 52

Answers (1)

SimonSays
SimonSays

Reputation: 10977

Do not use adapter.getView() for that! This method is used internally for the adapter to create the view that gets displayed in the list! The correct way to do this is to modify the underlying data and to refresh the list with adapter.notifyDataSetChanged(). Do not try to access views in the list directly, you don't know if they are visible at the moment or scrolled outside the view.

Upvotes: 5

Related Questions