0

The spinner at the top specifies the number of family members you have.

If 3 is selected then 3 rows should be created with text fields, number field, spinner all at once.

This is a screenshot of my app:

screenshot

activity_main3

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.technet_pc2.myapplication.MainActivity3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Family members"
        android:id="@+id/textView12"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:id="@+id/tablelayout1">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Sr No."
                android:id="@+id/textView14"
                android:layout_column="0"
                android:ems="2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Name"
                android:id="@+id/textView15"
                android:layout_column="1"
                android:ems="7"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Age"
                android:id="@+id/textView16"
                android:layout_column="2"
                android:ems="3"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Sex"
                android:id="@+id/textView17"
                android:layout_column="3"
                android:ems="2"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Relation"
                android:id="@+id/textView18"
                android:layout_column="4"
                android:ems="4"/>
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="1"
                android:id="@+id/textView19"
                android:layout_column="0" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText8"
                android:layout_column="1" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="2"
                android:id="@+id/editText9"
                android:layout_column="2" />

            <Spinner
                android:layout_width="1dp"
                android:layout_height="1dp"
                android:id="@+id/spinner3"
                android:layout_column="3" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText10"
                android:layout_column="4" />
        </TableRow>
    </TableLayout>

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner4"
        android:layout_below="@+id/textView12"
        android:layout_alignLeft="@+id/textView12"
        android:layout_alignStart="@+id/textView12"
        android:layout_alignRight="@+id/textView12"
        android:layout_alignEnd="@+id/textView12"
        android:spinnerMode="dropdown" />

</RelativeLayout>

MainActivity3.java

public class MainActivity3 extends AppCompatActivity {


    Button b1;
    TableLayout t1;
    Spinner sp;
    ArrayAdapter ad;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        b1=(Button)findViewById(R.id.button);
        sp=(Spinner)findViewById(R.id.spinner4);

     /*  b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TableRow row = new TableRow(MainActivity3.this);
                //add Layouts to your new row
                TextView txt = new TextView(MainActivity3.this);
                txt.setText("1");
                row.addView(txt);
                //add your new row to the TableLayout:
                TableLayout t1 = (TableLayout) findViewById(R.id.tablelayout1);
                t1.addView(row);
            }
        });*/

        t1 = (TableLayout)MainActivity3.this.findViewById(R.id.tablelayout1);

        ad=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
        ad.add("Select");
        ad.add("1");
        ad.add("2");
        sp.setAdapter(ad);



           if (sp.getSelectedItem().toString().equals("Select")) {

        }
        else if (sp.getSelectedItem().toString().equals("1")) {
            // Inflate your row "template" and fill out the fields.
           TableRow row = (TableRow) LayoutInflater.from(MainActivity3.this).inflate(R.layout.attrib_row, null);
               ((TextView)row.findViewById(R.id.textView19)).getText();
               t1.addView(row);





        }



    }
}

attrib_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow  xmlns:android="http://schemas.android.com/apk/res/android">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="1"
                android:id="@+id/textView19"
                android:layout_column="0" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText8"
                android:layout_column="1" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="2"
                android:id="@+id/editText9"
                android:layout_column="2" />

            <Spinner
                android:layout_width="1dp"
                android:layout_height="1dp"
                android:id="@+id/spinner3"
                android:layout_column="3" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText10"
                android:layout_column="4" />
</TableRow>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

You should add the row dynamically and not put them statically in the layout. To achieve this, you need to extract the dynamic row (the one with the EditText views) to another xml and once the selector selects the number of entries, inflate the right number of rows and add them to the table.

I suggest you have a look at the answers to this question and make the proper adaptations. Don't forget to give unique ids to each of the rows as you will be collecting the data or alternately use a the ViewHolder pattern to hold and handle each row and its data.

Community
  • 1
  • 1
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
0

You can use recyclerview with recycler item as this:

enter image description here

and then in OnitemClickListner of spinner you just need to update the count in the adapter or add the item to arraylist<CustomObject> and then call notifyDataSetChanged()

You can updated serial number as position+1 in the adapter

Satish Silveri
  • 393
  • 4
  • 9