0

I have a ScrollView with LinearLayout into it.After adding the Layout into my LinearLayout, I wanted to remove the view but it removes only the first view.

I have tried parent.removeView(myView)

My Code for Layout adding :

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

}

Any answer Appreciated...Thks

Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46

2 Answers2

0

The code

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

            }
        });

is in a for loop.

It means the click of the button will only remove the view_middle that was lastly added to the LinearLayout, as you are using the same object name.

bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
0

Something like below will solve your problem.

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

Edit: You are using same view_middle reference for all your view_middle istances inside your loop, so you can only remove last view_middle instance with your code.

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • @hi aegean .I clearly got your code. But view_middleArr = new View[jsonArray.length()]; I m asked to remove the final modifier ,which I can't as it is in Annons class of OnClickListener – Nitesh Tiwari Nov 07 '13 at 05:24
  • See why you have to use final: http://stackoverflow.com/questions/9857394/java-final-variable-requirement-in-onclicklistener – Devrim Nov 07 '13 at 06:17
  • Thks aegean. Great Answer works Like Ease.And about final keyword issue the Initilisation I have done: final View[] view_middleArr = new View[jsonArray.length()]; directly above the forLoop worked but not withIn the Loop...Once again GREAT – Nitesh Tiwari Nov 07 '13 at 06:32