Eliminar todos los elementos dentro linearlayout


Creo un linearlayout que se refiere a un elemento xml. Dentro de este linearlayout pongo algunos textview dinámicamente, así que sin tomarlos del xml. Ahora necesito eliminar estas textviews del linearlayout. Probé esto:

if(((LinearLayout) linearLayout.getParent()).getChildCount() > 0)
    ((LinearLayout) linearLayout.getParent()).removeAllViews();

Pero no funciona. ¿Cómo puedo hacerlo? Gracias, Mattia

Author: pindol, 2012-03-20

2 answers

Por qué escribiste linearLayout.getParent() debes hacer todo esto directamente en LinearLayout

if(((LinearLayout) linearLayout).getChildCount() > 0) 
    ((LinearLayout) linearLayout).removeAllViews(); 
 130
Author: MKJParekh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-20 10:53:02

Hola, por favor, intente este código está funcionando para mí

public class ShowText extends Activity {
    /** Called when the activity is first created. */
    LinearLayout linearLayout;
    TextView textView,textView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView=new TextView(this);
        textView1=new TextView(this);
        textView.setText("First TextView");
        textView1.setText("First TextView");

        linearLayout=(LinearLayout) findViewById(R.id.mn);
        linearLayout.addView(textView);
        linearLayout.addView(textView1);
        linearLayout.removeAllViews();

    }
}
 5
Author: Manju,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-20 11:05:07