mardi 5 juillet 2016

Two check boxes for single item calling position

My code is designed to have two check boxes for a single item in listview, and the problem is that when I clicked on the first checkbox, the other checkbox is clicked automatically, and also when I uncheck one of them the other one is unchecked automatically

I think the problem is that the two check boxes get the position of the item, so they have the same position, I need each one of the check boxes has it's own position

@Override public View getView(final int position, View convertView, ViewGroup parent) {
     ViewHolder holder=null;
    View itemView = convertView;
    if (itemView == null){
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.item, null);
        holder = new ViewHolder();
        holder.textView1  = (TextView) itemView.findViewById(R.id.textView1);
        holder.check1 = (CheckBox) itemView.findViewById(R.id.check1);
        holder.check2 = (CheckBox) itemView.findViewById(R.id.check2);
        itemView.setTag(holder);
   }
    else{
        holder = (ViewHolder) itemView.getTag();
    }
    holder.check1.setTag(position);
    final Item item = items.get(position);
    holder.textView1.setText(item.getName());
    holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               if(buttonView.isPressed()){
                    if (position != ListView.INVALID_POSITION) {
                        Item p = items.get(position);
                        p.setSelected(isChecked);
                    }
               }
          }
    }
);
holder.check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if(buttonView.isPressed()){
                 if (position != ListView.INVALID_POSITION) {
                       Item p = items.get(position);
                       p.setSelected(isChecked);
                 }
            }
       }
   }
    );
    holder.check1.setChecked(item.isSelected());
    holder.check2.setChecked(item.isSelected());
    return itemView;
}
}private static class ViewHolder {
public TextView textView1;
public CheckBox  check2,check1;
}}

Aucun commentaire:

Enregistrer un commentaire