I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.  

Specially for stackoverflow I build a small example of what I want to do.
To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).  

 After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID.
So I see two way of solution:
1)Change there ID dynamically  

 2)Something else))  

 Thank you very much!!!

This is 

> 
> sub\_fields.xml
> 
> 
> 

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="@+id/detailsLayout"
android:focusableInTouchMode="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtName"
        android:hint="name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/txtPhone"
        android:layout_gravity="center_horizontal"
        android:hint="phone" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD"
            android:id="@+id/btnAddd"
            android:onClick="onClickAddd" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="REMOVE"
            android:id="@+id/btnRemove"
            android:onClick="onClickAddd" />

    </LinearLayout>
</LinearLayout>

```

> 
> activity\_main.xml
> 
> 
> 

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="@+id/generalLayout">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="56dp"
    android:gravity="center"
    android:background="#7d65258a">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="FILL FIELDS"
        android:id="@+id/textView" />
</LinearLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/subLayoutFields">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtName"
        android:hint="name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/txtPhone"
        android:layout_gravity="center_horizontal"
        android:hint="phone" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD"
            android:id="@+id/btnAdd"
            android:onClick="onClickAdd" />

    </LinearLayout>
</LinearLayout>

```

> 
> MainActivity.java
> 
> 
> 

```
package andrey.adddinamicallycontrolsapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
       if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onClickAdd(View view) {
        LayoutInflater ltInflater = getLayoutInflater();
       final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
       final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
        Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);

        buttonRemove.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
            }});

    }

```