I setup this loop, and it works fine, but I would like to be able to change each textView individually, so I need to setup textView.setId(whateveryouputinhere); Could someone please explain to me how to setup the id and what you put inside the parenthesis? Thanks!
while (counter < 5) {
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.newplayerlayout, null);
parent.addView(view);
TextView textView = (TextView) view.findViewById(R.id.textView2);
textView.setText("Player "+counter);
textView.setId(counter);
counter++;
}
ok
no you don’t, there are better ways to achieve what you are trying to do.
One example might be something like this:
Note, I didn’t compile this but it should be close. If you get an error on the “this” change them to “YourActivity.this” with the name of your activity class.
There are a few benefits, by keeping the LayoutInflater reference you save creating 4 more of those. After you inflate the view object there is no need to then find it by Id, you’ve already got it. You can cast it as a TextView right out of the inflater if you like. By keeping them in an array you can always reference each of them without having to call findViewById() again, this method is rather expensive, you should try to avoid calling it excessively.