2

enter image description here 我有一個EditTextImageView Plus不刪除Arraylist中的特定項目

  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

試試這個,只需更換你的方法是這樣,享受

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

} 

希望這有助於和享受你的工作。

+0

查看編輯答案 –

+0

嗨,感謝您的回覆,但沒有給findviewbyid,它將如何識別刪除哪個視圖/內容? – DroidLearner

+0

這是一個大班嗎?你可以發佈代碼,我會爲你糾正嗎?我現在很急,所以沒有太多時間在黑暗中捅戳:) –