简体   繁体   中英

Remove all items inside linearlayout

I create a linearlayout that refes to an xml item. Inside this linearlayout i put some textview dynamically, so without taking them from the xml. Now i need to remove these textviews from the linearlayout. I tried this:

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

but it doesn't work. How can i do? Thanks, Mattia

Why you wrote linearLayout.getParent() ?

You should call this directly on LinearLayout :

linearLayout.removeAllViews();

Hi Please try this code its working for me

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();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM