5

I have a huge layout for my activity that i set using setContentView(). In that layout I have a TableLayout(named tableLayout in my Activity) that I want to populate. The rows of that TableLayout are customViews in a layout file(let's call that file tablerow_layout.xml). Inside this layout i have some TextViews and ImageView. What I want to do is change the content of the TextView programatically, when I create the row. This is my code:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView) newRow. findViewById(R.id.tablerow_name);
name.setText("THIS LINE");
tableLayout.addView(newRow);

Unfortunately my app crashes when I try to change the content of the TextView. I know that I could do this using a ListView, but I usually have a small number of rows and the overhead of creating another Adapter for the ListView is preatty big.

As @ρяσѕρєя K suggested I also tried newRow.findViewById(...) instead of findViewById(...) without any difference

Felix
  • 159
  • 2
  • 3
  • 13

4 Answers4

4

You are missing to set newRow in your Textview .

  View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
  TextView name = (TextView)newRow.findViewById(R.id.tablerow_name);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
3

How can I get a reference of a view from a inflated layout

Use View object which is returned from LayoutInflater.inflate method to access views from layout which is passed as first parameter to inflate method.

In your case, use newRow object for accessing views from R.layout.tablerow_layout layout:

TextView name = (TextView)newRow.findViewById(R.id.tablerow_name); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
3
  try

   {
   LayoutInflater layoutInflater = (LayoutInflater)   
   getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null,   false);
     TextView name = (TextView) newRow.findViewById(R.id.tablerow_name);

     name.setText("THIS LINE");

    tableLayout.addView(newRow);

    }catch(Exception e)

    {

    e.printTracktrace();

    }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
2

Using IntelliJ Amiya's and ρяσѕρєя K's answers it worked for me with this code.

LayoutInflater inflater = LayoutInflater.from(this);
    LinearLayout linearLayout = 
(LinearLayout)findViewById(R.id.profileView);
    ViewGroup viewGroup = linearLayout;

    for (int i = 0; i < NUM_OF_PROFILES; i++){

        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.profile_badge_layout, viewGroup, false);
        linearLayout.addView(layout);

        TextView names = (TextView)layout.findViewById(R.id.profileName);
        ImageView images = (ImageView)layout.findViewById(R.id.profileImage);

        names.setText(""+i);


        viewGroup = layout;
    }

Each text had it's own i value. I believe you need to add the view first. Then you can change the values