How do I remove a view created in the java code by the Id of this view?

2
How do I remove a view by the ID? Because in my application it has an "add" button to add a group of components that "group" which is a LinearLayout has a field and an "X" button of delete. When you press the "X" button it has to delete only the field that is on the side of it. But only get it to delete the last group, because .removeView does not remove by the "Id" of the group. Can anyone help me if it is possible?

xml code:

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

<LinearLayout        
    android:id="@+id/lnr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

    <!-- Botão "+" para add campos -->
    <Button
        android:id="@+id/add"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="end"
        android:text="+"
        android:textSize="30sp" />

    <!-- Container -->
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >                     

    </LinearLayout>        

</LinearLayout>

Java code:

package com.example.teste;

import android.support.v7.app.ActionBarActivity;

import android.annotation.SuppressLint; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout;

@SuppressLint ("NewApi") public class MainActivity extends ActionBarActivity implements OnClickListener {

int id = 1;
Button add, del;
EditText campo;
LinearLayout grupo, container;

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

    add = (Button)findViewById(R.id.add);
    add.setOnClickListener(this);

    container = (LinearLayout)findViewById(R.id.container);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {

    campo = new EditText(this);
    campo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    campo.setText(" Campo  " + id + " ............");

    del = new Button(this);
    del.setLayoutParams(add.getLayoutParams());
    del.setText("X");
    del.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            container.removeView(grupo);
        }
    });

    grupo = new LinearLayout(this);
    grupo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    if (v.getId() == R.id.add) {

        campo.setId(id);
        del.setId(id);
        grupo.setId(id);

        grupo.addView(campo);
        grupo.addView(del);

        container.addView(grupo);

        id = id + 1;

    }

}

}

    
asked by anonymous 07.10.2015 / 20:03

1 answer

1

The problem with the code is to always use the same reference to the object "group", to solve this, just create a new object every time the function is activated.

MainActivity.java

package com.example.teste;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements OnClickListener {

private LinearLayout mContainer;
private int mIncrementalId = 1;
private Button mAddButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Buscando container que recebe novas linhas
    setContentView(R.layout.activity_main);
    mContainer = (LinearLayout) findViewById(R.id.container);
    //Buscando botao para usar layout
    mAddButton = (Button) findViewById(R.id.add);
    mAddButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    //Criando novo grupo de linhas
    //Sempre usando um novo objeto para o grupo
    final LinearLayout novoGrupo = new LinearLayout(this);
    novoGrupo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    //Criando novo campo de entrada de texto
    EditText novoCampo = new EditText(this);
    novoCampo.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    novoCampo.setText(" Campo  " + mIncrementalId + " ............");

    //Criando novo botao responsavel por remover o novo grupo
    Button delButton = new Button(this);
    delButton.setLayoutParams(mAddButton.getLayoutParams());
    delButton.setText("X");
    delButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mContainer.removeView(novoGrupo);
        }
    });

    //Adicionando views ao grupo
    novoGrupo.addView(novoCampo);
    novoGrupo.addView(delButton);

    //Adicionando a view principal
    mContainer.addView(novoGrupo);

    //Incrementando id
    mIncrementalId++;


}

}

content_main.xml    

<LinearLayout
    android:id="@+id/lnr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <!-- Botão "+" para add campos -->
    <Button
        android:id="@+id/add"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="end"
        android:onClick="onClickAdd"
        android:text="+"
        android:textSize="30sp" />


    <!-- Container -->
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

    
08.10.2015 / 22:19