Search code examples
androidandroid-layoutlayout-inflater

Android: setting text in each new view within a for loop


So I have an XML layout1 which is just a LinearLayout with three text views. I also have another XML layout2 with ScrollView and a LinearLayout inside it. I'm using this for loop to create several of the layout2 inside the LinearLayout of the ScrollView. It's working fine but I want to be able to set the text of each of the TextViews within the for loop. I'm not sure how to access these TextViews as I can only set one id within the XML file, will that not cause problems if I tried to access their id inside the for loop?

private void setUpResults() {
        for (int i = 1; i < totalQuestions; i++) {
            parent.addView(LayoutInflater.from(getBaseContext()).inflate(
                    R.layout.result_block, null));

        }
    }

Here is the result_block xml file (layout1) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layoutSelectedAnswer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/option_padding_bottom"
        android:paddingTop="@dimen/option_padding_top" >

        <TextView
            android:id="@+id/tvOptionALabel2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/option_a"
            android:textColor="@color/white"
            android:textSize="@dimen/option_text_size" />

        <TextView
            android:id="@+id/tvSelectedAnswer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/option"
            android:textColor="@color/white"
            android:textSize="@dimen/option_text_size" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutCorrectAnswer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/option_padding_bottom"
        android:paddingTop="@dimen/option_padding_top" >

        <TextView
            android:id="@+id/tvOptionBLabel2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/option_b"
            android:textColor="@color/white"
            android:textSize="@dimen/option_text_size" />

        <TextView
            android:id="@+id/tvCorrectAnswer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:text="@string/option"
            android:textColor="@color/white"
            android:textSize="@dimen/option_text_size" />
    </LinearLayout>

</LinearLayout>

Let's say I wanted to set the TextView with the id as tvCorrectAnswer to a different String value in each loop, how should I access it?


Solution

  • Sure, you can do it like this:

    private void setUpResults () {
        LayoutInflater i = LayoutInflater.from(getBaseContext());
    
        for (int i = 1 /* should be zero? */; i < totalQuestions; i++) {
            View view = i.inflate(R.layout.result_block, parent, false);
            TextView correctAnswer = (TextView) view.findViewById(R.id.tvSelectedAnswer);
            correctAnswer.setText("My Answer Text");
            parent.addView(view);
        }
    }
    

    The key is, inflate using the parent as the container, but don't attach it (the false parameter). Then you'll get a reference to the inflated view, which you can then directly reference to do your findViewById() calls (which will limit the search to that particular ViewGroup). Then add it to the parent and continue to the next item.