Question

I have a GridLayout to show players data in a ListView Activity (In a future I probably change to a really ListView, but for the purpose of this question think that is mandatory to be a GridLayout).

In an activity result I have to add new rows to the layout and as all rows have the same components, I put all row components in a new layout file with a merge parent and when need to add a row simply inflate with the GridLayout as rootView and attach to true.

The problem is when I add a new row as the components id are the same, all the time I change the properties (text of a TextView) of the first row.

How can I access the other views with the same id?

Était-ce utile?

La solution

I have search many times and the only thing that I found is that You can't do it.

So I have to get alternatives like:

  • Add then directly in the code. So you mess code and UI.
  • Change <merge> element to other. So it's inefficient.
  • Use the index to get the view instead of the ID. Again you mess code and UI, but a little less than with first option.

In my case I have used the index letting me with code like:

inflater.inflate(R.layout.player_in_match, llPlayers, true);

// 4 Is because I have 4 elements in the R.layout.player_in_match
// and I wanna get the first of them.
TextView tvName = (TextView) llPlayers.getChildAt(llPlayers.getChildCount() - 4);

tvName.setText(name);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top