Pregunta

I have spent a fair amount this morning trying to learn how to inflate an activity with the same row several times. I created an xml file called row per person.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/enterName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <TextView
        android:id="@+id/tipPerPerson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="$      " />
</TableRow>

and it my activity BILL_ENTRY_SCREEN, I entered the following code.

        rowPerPerson = (TableRow) findViewById(R.id.divideTips);

    TableLayout table = (TableLayout)BILL_ENTRY_SCREEN.this.findViewById(R.id.table);

    for(int i = 1; i <4; i++) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.row_per_person, table);
    }

However instead of the row showing up 4 times it only show up once. What am I doing wrong.

¿Fue útil?

Solución

For each i you have to create a new instance for the TableRow View and then add it to the TableLayout.

So you code should be like this :

for(int i = 1; i <=4; i++) {
     row = new TableRow(this);
     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     inflater.inflate(R.layout.row_per_person, table); 
     row.addView(inflater);     // add view to row
     table.addView(row);  // add to table
 }

Otros consejos

Here is your answer: Use view to inflate multiple times

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top