2012-03-20 19 views
44

Creo una distribución lineal que se refiere a un elemento xml. Dentro de esta distribución lineal pongo un poco de vista de texto de forma dinámica, sin tomarlos del xml. Ahora necesito eliminar estas textviews de linearlayout. Intenté esto:Eliminar todos los elementos dentro de linearlayout

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

pero no trabaja. ¿Cómo puedo hacer? Gracias , Mattia

Respuesta

115

por qué escribió linearLayout.getParent() que debe hacer todo esto directamente en LinearLayout

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

Hola Por favor, intente este código de su trabajo 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(); 

    } 
} 
Cuestiones relacionadas