سؤال

I've set up an xml layout for an android activity, its consists of a LinearLayout which holds a ScrollView, a TableLayout with two Buttons.

The ScrollView itself contains an other TableLayout with a default number of TableRows, let's say six. The TableRows are all of the same type - three EditText widgets.

I would like to give the user the option to add or remove TableRows which should then integrate into the TableLayout.

I know how to add Widgets to a layout but not really how to integrate them into an existing xml structure!

So my question is how to specify the layout parent for the addView() method, so that the TableRows and EditTexts would be added/removed as the last ScrollView item.

I would like to use a "counter" method to monitor the number of TableRows and to provide the IDs for the next item.

An actual Row should look like this and each new one should be a copy of it with different IDs

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

            <EditText
                android:id="@+id/MainExercise1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="@string/MainExerciseHint" />

            <EditText
                android:id="@+id/MainReps1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:ems="10"
                android:hint="@string/MainRepsHint" />

            <EditText
                android:id="@+id/MainWeight1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:ems="10"
                android:hint="@string/MainWeightHint" />
        </TableRow>
هل كانت مفيدة؟

المحلول

// get the viewgroup I suppose it's tablelayout in your case    
TableLayout tl =(TableLayout) rootView.findViewById(R.id.your_table_layout);

//create a new tablerow with attributes you want
TableRow r = new TableRow(getActivity());

//create edittext as much as you want
EditText e1 = new EditText(getActivity());

//add edittext to tablerow
r.addView(e1);

//add tablerow to tablelayout
tl.addView(r);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top