0

我有一个ScrollViewwith 。LinearLayout在将 Layout 添加到 myLinearLayout之后,我想删除该视图,但它只删除了第一个视图。

我努力了parent.removeView(myView)

我的布局代码添加:

LayoutInflater inflater = getLayoutInflater();

final View view_top = inflater.inflate(R.layout.layout_top, linear_layout,false);
final View view_bottom = inflater.inflate(R.layout.layout_bottom,linear_layout,false);


final RelativeLayout rel_layout_bottom = (RelativeLayout) view_bottom.findViewById(R.id.relative_bottom);

Button btn_update = (Button) rel_layout_bottom.findViewById(R.id.lst_todo_update);

ImageButton btn_remove = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_delete);

ImageButton btn_Color = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_color);


final RelativeLayout rel_layout_top = (RelativeLayout) view_top.findViewById(R.id.relative_top );
final TextView note_Title = (TextView) rel_layout_top.findViewById(R.id.lst_title );
final TextView note_color = (TextView) rel_layout_top.findViewById(R.id.lst_color_type );

note_Title.setText(note_title);

linear_layout.addView(rel_layout_bottom, 0);


JSONArray jsonArray=queryResult.getResultObjects().get(count).getJSONArray("todo_item");

for (int i = 0; i < jsonArray.length(); i++) {


    final View view_middle  = inflater.inflate(R.layout.layout_middle, linear_layout, false);

    final RelativeLayout rel_layout_middle = (RelativeLayout) view_middle.findViewById(R.id.relative_middle);

    final CheckBox note_check ;

    final TextView note_content ;

    note_content = (TextView) rel_layout_middle.findViewById(R.id.lst_content);
    note_check = (CheckBox) rel_layout_middle.findViewById(R.id.lst_check);
    btn_remove.setOnClickListener(null);
    try {

        //Getting data correctly here

        linear_layout.addView(view_middle,0);

        btn_remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {               

               linear_layout.removeView(view_middle);        //Not able to remove the view here i.e view_middle
               linear_layout.removeView(view_top);
               linear_layout.removeView(view_bottom);

            }
        }); 


    } catch (JSONException e) {

        e.printStackTrace();
    }

}

linear_layout.addView(rel_layout_top , 0);

}

任何答案表示赞赏...谢谢

4

2 回答 2

0

像下面这样的东西会解决你的问题。

final View[] view_middleArr;
for (int i = 0; i < jsonArray.length(); i++) {
    view_middleArr = new View[jsonArray.length()];
    view_middleArr[i]  = inflater.inflate(R.layout.layout_middle, linear_layout, false);
    // ...

        btn_remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {               

                for (View view_middle : view_middleArr) {
                     linear_layout.removeView(view_middle);  
                }                           
               linear_layout.removeView(view_top);
               linear_layout.removeView(view_bottom);
            }
        }); 
}

编辑:您对循环内的view_middle所有实例都使用相同的引用,因此您只能使用代码删除最后一个实例。view_middleview_middle

于 2013-11-06T13:45:22.223 回答
0

编码

btn_remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {               

               linear_layout.removeView(view_middle);        //Not able to remove the view here i.e view_middle
               linear_layout.removeView(view_top);
               linear_layout.removeView(view_bottom);

            }
        });

处于 for 循环中。

这意味着单击按钮只会删除view_middle最后添加到 的LinearLayout,因为您使用的是相同的对象名称。

于 2013-11-06T13:40:56.667 回答