DroidLearner
DroidLearner

Reputation: 2135

Not removing particular item in Arraylist

enter image description here I have a EditText with ImageView Plus .

  1. On click of ImageView Plus I'm inflating new layout with EditText and ImageView Minus.

  2. Now, On Click of ImageView Minus. I want to remove the inflated layout. How to do this?

    ArrayList<View> viewList;
    /* Inflating new Layout */
    case R.id.ivPlus:
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        addView = layoutInflater.inflate(R.layout.add_edit, null);
        viewList.add(addView);
        ll.addView(addView);
        break;
    /* Removing the inflated layout */
    for (int i = 0; i < viewList.size(); i++) {
        final int j = i;
        ImageView minus= (ImageView ) viewList.get(j).findViewById(R.id.ivMinus);
        minus.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                LinearLayout extra_add = (LinearLayout) findViewById(R.id.lin_add);
    
                extra_add.removeViewAt(j);
            }
        });
    }
    

Upvotes: 2

Views: 166

Answers (1)

Marko Lazić
Marko Lazić

Reputation: 883

Try this, just replace your method like this and enjoy

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_add:
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View addView = layoutInflater
                .inflate(R.layout.inflate_layout, null);
        viewList.add(addView);
        lin_layout.addView(addView);
        Button remove = (Button) addView.findViewById(R.id.button_remove);
        remove.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                lin_layout.removeView((View) v.getParent());
                viewList.remove((View) v.getParent());
            }
        });
        break;
    }

}

Hope this helps and enjoy your work.

Upvotes: 1

Related Questions