2014-02-05 245 views
0

我有一個自定義適配器類,如下所示。當我選擇一個複選框時,其他複選框也會在7-8行之後被選中。即使setonclicklisterner沒有實現,複選框的行爲也是一樣的。我需要一個適當的解決方案我搜索了很多,但沒有得到解決方案。 如何停止避免多選複選框?當選中一個複選框時,多個複選框會被選中?

public class CustomAdapter extends ArrayAdapter<Custom>{ 
    private ArrayList<Custom> entries; 
    private Activity activity; 
    private Context context; 
    private qrusers qrusers; 

    private String[] udis; 
    ArrayList<String> userid= new ArrayList<String>(); 
    boolean[] checkBoxState; 
    LayoutInflater vi; 
    public CustomAdapter(Context context, ArrayList<Custom> entries,String []udis) { 
     super(context,0,entries); 
     this.entries = entries; 
     this.context= context; 
     this.udis=udis; 
     checkBoxState=new boolean[entries.size()]; 
     this.qrusers =(qrusers) context; 
     vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public static class ViewHolder{ 
     public TextView item1; 
     public TextView item2; 
     public CheckBox chek; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; 
     if (v == null) { 

      v = vi.inflate(R.layout.checkboxlist, null); 
      holder = new ViewHolder(); 
      holder.item1 = (TextView) v.findViewById(R.id.contactname); 
      holder.item2 = (TextView) v.findViewById(R.id.companyname); 
      holder.chek=(CheckBox)v.findViewById(R.id.checboxlist); 

      holder.chek.setTag(udis[position]); 


      v.setTag(holder); 
      /* holder.chek.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       private String s; 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        // TODO Auto-generated method stub 
        if(isChecked){ 
         checkBoxState[position]=true; 
         // isChecked=true; 
         s=(String)buttonView.getTag(); 
         Log.e("IDDDDDDDD", s); 

         userid.add(s); 
       Log.e("ADDED ID", userid.toString()); 
       } 
        else{ 
         checkBoxState[position]=false; 
         s=(String)buttonView.getTag(); 
         userid.remove(s); 
         Log.e("Removed ID", userid.toString()); 
        } 
        } 
      });*/ 
      holder.chek.setOnClickListener(new OnClickListener() { 
       private String s; 
       @Override 
       public void onClick(View v) { 
        if (((CheckBox) v).isChecked()){ 
         checkBoxState[position]=true; 
         // isChecked=true; 
         s=(String)v.getTag(); 
         Log.e("IDDDDDDDD", s); 

         userid.add(s); 
       Log.e("ADDED ID", userid.toString()); 
       } 
        else{ 
         checkBoxState[position]=false; 
         s=(String)v.getTag(); 
         userid.remove(s); 
         Log.e("Removed ID", userid.toString()); 
        } 


       } 
      }); 
      holder.chek.setChecked(checkBoxState[position]); 
      CheckBox result = (CheckBox)convertView; 
      if (result == null) { 
       result = new CheckBox(context); 
      }} 

     else 

      holder=(ViewHolder)v.getTag(); 

     final Custom custom = entries.get(position); 
     if (custom != null) { 
      holder.item1.setText(custom.getcustomBig()); 
      holder.item2.setText(custom.getcustomSmall()); 

     } 
     return v; 
    } 
    public ArrayList<String> getUserid() { 
     return userid; 
    } 
    public void setUserid(ArrayList<String> userid) { 
     this.userid = userid; 
    } 
} 
+0

嘗試使用本教程保存數組中選中的複選框的值。 http://sunil-android.blogspot.in/2013/04/android-listview-checkbox-example.html –

回答

0

將此代碼用於基礎適配器。

public class CollageView extends BaseAdapter { 
    Context context; 
    ArrayList<String> image_arrylist = new ArrayList<String>(); 
    File file; 
    Bitmap bm; 
    LayoutInflater inflater; 
    ImageView imageview; 
    ViewHolder holder = null; 
    int position; 

    public CollageView(Context c, ArrayList<String> array_list) { 
     context = c; 
     imageview = new ImageView(c); 
     image_arrylist = array_list; 
     inflater = LayoutInflater.from(context); 

     System.out.println("Size-&&7" + image_arrylist.size()); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return image_arrylist.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 

     View v = convertView; 
     if (v == null) { 
      v = inflater.inflate(R.layout.collage_image_row, null); 
      holder = new ViewHolder(); 
      holder.image = (ImageView) v 
        .findViewById(R.id.imageView_collage); 
      holder.checkbox = (CheckBox) v 
        .findViewById(R.id.itemCheckBox_collage_image); 
      v.setTag(holder); 
     } else { 
      holder = (ViewHolder) v.getTag(); 
     } 
     holder.checkbox.setId(position); 
     holder.image.setId(position); 

     if (CollageViewActivity.checkbox_boolean == true) { 
      holder.checkbox.setVisibility(View.VISIBLE); 
     } 

     System.out.println("aname" + aname); 

     holder.checkbox.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

       System.out.println("click-1"); 

       // TODO Auto-generated method stub 
       CheckBox cb = (CheckBox) v; 
       int id = cb.getId(); 
       if (thumbnailsselection[id]) { 
        cb.setChecked(false); 
        thumbnailsselection[id] = false; 
       } else { 
        cb.setChecked(true); 
        thumbnailsselection[id] = true; 
       } 
      } 
     }); 

     holder.checkbox.setChecked(thumbnailsselection[position]); 
     holder.id = position; 

     System.out.println("set image..." + image_arrylist.get(position)); 
     file = new File(image_arrylist.get(position)); 
     String ext = file.toString(); 
     if (checkVideo(file)) { 
      System.out.println("video exist " + file.exists()); 
      holder.image.setScaleType(ImageView.ScaleType.CENTER); 
      aq.id(holder.image).image(
        getVideoThumnail(image_arrylist.get(position))); 
     } else { 
      System.out.println("file exist " + file.exists()); 
      holder.image.setScaleType(ImageView.ScaleType.CENTER); 
      aq.id(holder.image).image(file, 300); 
     } 

     holder.image.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       System.out.println("labelorcollage" 
         + GeneralClass.lableorcollage 
         + image_arrylist.size()); 

       GeneralClass.checkfullimage = true; 
       // gridview.setVisibility(View.GONE); 
       playout_image.setVisibility(View.VISIBLE); 
       playout_grid.setVisibility(View.GONE); 

      } 
     }); 

     return v; 
    } 

    public class ViewHolder { 
     TextView imageTitle; 
     ImageView image; 
     RelativeLayout layout; 
     CheckBox checkbox; 
     int id; 
    } 
} 
0
//try to replace this peace of code 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; 
     if (v == null) { 
      v = vi.inflate(R.layout.checkboxlist, null); 
      holder = new ViewHolder(); 
      holder.item1 = (TextView) v.findViewById(R.id.contactname); 
      holder.item2 = (TextView) v.findViewById(R.id.companyname); 
      holder.chek=(CheckBox)v.findViewById(R.id.checboxlist); 
      v.setTag(holder); 
     }else{ 
       holder=(ViewHolder)v.getTag(); 
     } 
      holder.chek.setChecked(checkBoxState[position]); 
      holder.chek.setTag(position); 
      holder.chek.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
       checkBoxState[(Integer)((CheckBox)compoundButton).getTag()]=b; 
       if(b){ 
        userid.add(udis[(Integer)((CheckBox)compoundButton).getTag()]); 
       }else{ 
        userid.remove(udis[(Integer)((CheckBox)compoundButton).getTag()]); 
       } 
      } 
      }); 


     final Custom custom = entries.get(position); 
     if (custom != null) { 
      holder.item1.setText(custom.getcustomBig()); 
      holder.item2.setText(custom.getcustomSmall()); 

     } 
     return v; 
} 
+0

現在,當向下滾動時,複選框會被取消選中。 – user2699728

+0

添加此v.setTag(持有人);在返回v之前;聲明並讓我知道... –

相關問題