Android: LayoutInflater and findViewById woes

RedactedProfile picture RedactedProfile · Jan 21, 2012 · Viewed 7.4k times · Source

I'm trying to use LayoutInflater in a loop, but each time one of these guys is instantiated, I need to modify at least 2 TextViews found within each instantiation. What i currently have at the moment instantates appropriately and adds the inflated xml view just like I want it to, but the result is that only the first instatiaton's resources are getting modified, and therefore it just gets overwritten many times.

            LayoutInflater inflater;
            TextView eTitle;

            for(String[] episode : episodes) {

                //Log.d("Episodes", episode[0] + " / " + episode[2]);
                inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.episode_list_item, listView);
                eTitle = (TextView) findViewById(R.id.episode_list_item_title);

                eTitle.setText(episode[2]);
            }

Example output of my list:

  • 04 - Episode 04
  • Episode Title
  • Episode Title
  • Episode Title

where its supposed to look like this:

  • 01 - Episode 01
  • 02 - Episode 02
  • 03 - Episode 03
  • 04 - Episode 04

what do i need to do to ensure that im getting the corresponding unique textview id's from my inflated layouts?

Answer

user634545 picture user634545 · Jan 21, 2012

Why don't you use ListView instead?

 LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(String[] episode : episodes) {
    View view = inflater.inflate(R.layout.episode_list_item, null);
    mTitle = (TextView) view.findViewById(R.id.episode_list_item_title);
    mTitle.setText(episode[2]);

listView.addView(view);

}