web-dev-qa-db-ja.com

linearlayout内のすべてのアイテムを削除する

XMLアイテムを参照するlinearlayoutを作成します。このlinearlayout内には、テキストビューを動的に配置するため、xmlから取得しません。今、私はlinearlayoutからこれらのテキストビューを削除する必要があります。私はこれを試しました:

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

しかし、それは機能しません。どのようにできるのか?ありがとう、マティア

59
pindol

linearLayout.getParent()を書いた理由は、これらすべてをLinearLayoutで直接行う必要があります

if(((LinearLayout) linearLayout).getChildCount() > 0) 
    ((LinearLayout) linearLayout).removeAllViews(); 
146
MKJParekh

こんにちは私のためにこのコードを試してください

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
Manju