Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: LayoutInflater and findViewById woes

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?

like image 424
RedactedProfile Avatar asked Jan 17 '23 17:01

RedactedProfile


2 Answers

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);

}
like image 139
user634545 Avatar answered Feb 01 '23 17:02

user634545


I am assuming you have a few TextViews in your LinearLayout. One point is they can't have the same ids. So suppose you have 4 TextView , then give each different ids, say tv1, tv2 etc.

Now in your onCreate method, initialize all these textViews as:

myTextView1= (TextView)findViewByid(R.id.tv1);

etc etc...

In the function where you get the "episodes" try this:

`myTextView1.setText(episodes[0]);

myTextView2.setText(episodes[1]);

You don't have to use any inflater because its already there in the activities layout file, which is automatically done in onCreate.

like image 37
aqs Avatar answered Feb 01 '23 18:02

aqs