不删除Arraylist中的特定项目

时间:2013-05-20 07:04:30

标签: android arraylist android-linearlayout android-imageview android-edittext

enter image description here    我有EditText ImageView Plus

  1. 点击ImageView Plus我正在使用EditTextImageView Minus对新布局进行充气。

  2. 现在,点击ImageView Minus。我想删除膨胀的布局。怎么做?

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

1 个答案:

答案 0 :(得分:1)

试试这个,只需替换这样的方法并享受

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

}

希望这有助于并享受您的工作。