Android: LayoutInflater and findViewById woes

I try to use LayoutInflater in a loop, but every time one of these guys is created, I need to change at least 2 TextViews found in each instance. What I now find at the moment matches and adds a bloated xml look just like I want, but the result is that only the first instatiaton resources change, and so 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]); } 

An example of the output of my list:

  • 04 - Episode 04
  • Episode title
  • Episode title
  • Episode title

where it should look like this:

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

what do i need to do to im get the corresponding unique text id from my bloated layouts?

+4
source share
4 answers

Why aren't you using 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); } 
+8
source

I assume that you have several TextView in your LinearLayout . One thing is that they cannot have the same id s. Suppose you have 4 TextView , then specify id s each time, say tv1 , tv2 , etc.

Now in your onCreate method onCreate initialize all these text elements as:

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

etc. etc.

In the function where you get the "episodes", try the following:

`myTextView1.setText (episodes [0]);

myTextView2.setText (episodes [1]);

You do not need to use inflater because it is already present in the layout file, which is automatically executed in onCreate .

+1
source

Change the lines

 inflater.inflate(R.layout.episode_list_item, listView); eTitle = (TextView) findViewById(R.id.episode_list_item_title); 

in lines:

 eTitle =inflater.inflate(R.layout.episode_list_item, listView, false); listView.addChild(eTitle ); 

Single-line construction View eTitle =inflater.inflate(R.layout.episode_list_item, listView, true); just never works in practice.

And aqs is right (the second time a day when I compliment him), create an identifier dynamically, your xml layouts for eTitle should be id-free if you are waiting for many of them.

0
source

I believe that you forget to grab the opinion that it was bloated, and then looking at the new control in that view.

Here is your original code:

 inflater.inflate(R.layout.episode_list_item, listView); eTitle = (TextView) findViewById(R.id.episode_list_item_title); 

I think your code should look more like this (find the representation contained in an inflated form):

 View new_view = inflater.inflate(R.layout.episode_list_item, listView, false); eTitle = (TextView) new_view.findViewById(R.id.episode_list_item_title); eTitle.setText(episode[2]); listView.addView(new_view); 

This is what my code earned as expected.

Hope this helps!

John

0
source

Source: https://habr.com/ru/post/1392248/


All Articles