2012-09-19 45 views
1

你好我想刪除元素時checkbox未選中itemonsetchenge listner中。 這裏是我的代碼如何從複選框中取消選中時從數組中刪除元素?

checkbox_timeslot.clear(); 

      chk.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 

       String chetimeslot; 

       if(isChecked){   

       chetimeslot = (String)chk.getTag(); 
       checkbox_timeslot.add(chetimeslot);    
       Log.e("array value", ""+checkbox_timeslot);    
       Log.e("checked slo value", ""+chetimeslot); 

       }else{   

       checkbox_timeslot.add(""); 
       Log.e("else array value", ""+checkbox_timeslot);  

      }   
     } 

     }); 

「checkbox_timeslot」是我的字符串數組list.i要從checkbox..how的列表選中時,纔有可能幫助去除複選框未被選中的元素並加入項目?

+0

你解決它呢? –

+0

是的,解決了它。你可以告訴我如何計算多少複選框用戶檢查? – Google

回答

7
if(isChecked){   

    chetimeslot = (String)chk.getTag(); 
    checkbox_timeslot.add(chetimeslot);    
    Log.e("array value", ""+checkbox_timeslot);    
    Log.e("checked slo value", ""+chetimeslot); 

} else{   

    chetimeslot = (String)chk.getTag(); 
    checkbox_timeslot.remove(chetimeslot); 
    Log.e("else array value", ""+checkbox_timeslot);  

} 
+0

嘿,沒有得到你的答案的解決方案,最後我解決了它,我已經把它.. – Google

+0

@GooGLE好吧,我忘了設置標籤再次刪除ArrayList中的元素。偉大的工作夥計。 –

+0

嘿,你可以告訴我怎樣才能算出多少複選框用戶檢查? – Google

0

使用add(item)將一個元素添加到數組列表中。刪除(item)從列表中刪除。

chetimeslot = (String)chk.getTag(); 
if(isChecked){ 

    checkbox_timeslot.add(chetimeslot); 

} else{   

    checkbox_timeslot.remove(chetimeslot); 

} 
0
 if(isChecked){ 

      chetimeslot = (String)chk.getTag(); 
      checkbox_timeslot.add(chetimeslot); 

     }else{ 

      chetimeslot = (String)chk.getTag(); 
      checkbox_timeslot.remove(chetimeslot); 
     } 
} 
0

同樣的問題,我今天拿到。這就是我解決它的方法。 我發佈我的整個適配器類使用RecyclerView。

package com.aqua.machinetest; 

import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckBox; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Danish.sharma on 5/9/2017. 
*/ 

public class GovernateListAdapter extends RecyclerView.Adapter<GovernateListAdapter.MyViewHolder> { 

    private List<Governate> governateList; 
    public static ArrayList<String> arrayListSelectedContacts = new ArrayList<>(); 
    Context context; 

    public GovernateListAdapter(List<Governate> governateList, Context context) { 
     this.governateList = governateList; 
     this.context = context; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.governate_list_row, parent, false); 
     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 
     Governate governate = governateList.get(position); 
     holder.title.setText(governate.getName()); 

     holder.checkBox.setChecked(governateList.get(position).getCheckedBox()); 
     holder.checkBox.setTag(governateList.get(position)); 

     holder.checkBox.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 

       CheckBox cb = (CheckBox) v; 
       Governate governate1 = (Governate) cb.getTag(); 

       governate1.setCheckedBox(cb.isChecked()); 
       governateList.get(position).setCheckedBox(cb.isChecked()); 
       System.out.println("selected contacts list " + governate1.getId()); 

       //Toast.makeText(v.getContext(), contact.getName() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show(); 

       if (cb.isChecked()) { 
        arrayListSelectedContacts.add(governateList.get(position).getId()); 
       } else { 
        arrayListSelectedContacts.remove(governateList.get(position).getId()); 
       } 

       System.out.println("selected contacts list 3 " + arrayListSelectedContacts); 
       Toast.makeText(context, arrayListSelectedContacts.size() + " Added", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    } 

    @Override 
    public int getItemCount() { 
     return governateList.size(); 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView title; 
     public CheckBox checkBox; 


     public MyViewHolder(View view) { 
      super(view); 
      title = (TextView) view.findViewById(R.id.textView_Title); 
      checkBox = (CheckBox) view.findViewById(R.id.checkBox); 
     } 
    } 
} 

這是我的POJO類:

package com.aqua.machinetest; 

import android.os.Parcel; 
import android.os.Parcelable; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Danish.sharma on 5/9/2017. 
*/ 

public class Governate { 
    String id; 
    String name; 
    Boolean checkedBox = false; 
// List<Governate> governateList = new ArrayList<>(); 

    public Governate() { 

    } 

    public Governate(String id, String name) { 
     this.id = id; 
     this.name = name; 
     // this.governateList = governateList; 
    } 

    protected Governate(Parcel in) { 
     id = in.readString(); 
     name = in.readString(); 
    } 



    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Boolean getCheckedBox() { 
     return checkedBox; 
    } 

    public void setCheckedBox(Boolean checkedBox) { 
     this.checkedBox = checkedBox; 
    } 


} 

在最後:這是我的片段,其中我執行onClickListener

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.linear_Next: 
      try { 
       if (GovernateListAdapter.arrayListSelectedContacts.size() == 0) { 
        Toast.makeText(getActivity(), "No data selected.", Toast.LENGTH_SHORT).show(); 

       } else if (GovernateListAdapter.arrayListSelectedContacts.size() > 0) { 
        Toast.makeText(getActivity(), "Selected data=" + GovernateListAdapter.arrayListSelectedContacts.size(), 
          Toast.LENGTH_SHORT).show(); 



       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      break; 
    } 
} 
相關問題