开发者

remove dynamic radio buttons

Does anyone have a quick and easy method for removing dynamically added buttons from a Linear Layout开发者_StackOverflow社区 in Android? They seem to be kept in the saved instancestate and I don't want them when I return to the activity.


You may clear ALL views in a linear layout by using the following code:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.your_linear_layout);
myLayout.removeAllViews();

However, if you are looking to remove only the views that were dynamically added (and you have views in there that are not) this will not work.

If you need to do it this way you can do something like this

        LinearLayout l = (LinearLayout)findViewById(R.id.linearLayout);
        List<View> removeViews = new ArrayList<View>();
        int count = l.getChildCount();
        for (int i = 0; i < count; i++) {
            View v = l.getChildAt(i);
            if (v != null && v.getTag() != null
                    && v.getTag().toString().equals("dynamicView")) {
                removeViews.add(v);
            }
        }

        for (View v : removeViews) {
            l.removeView(v);
        }

Please notice the v.getTag() != null && v.getTag().toString().equals("dynamicView") portion. You don't have to do it this way, however, this would be an easy way to differentiate between a view you added and a view that was statically created.

Edit in order for this to work when you create the view you need to call view.setTag("dynamicView"); of course

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜