简体   繁体   中英

Android : checkbox automatically unchecked after scrolldown to listview

Here is my contact picker list activity which contain list of contacts. i want to pick multiple contacts via checkbox and send data to them. The Problem is : when i checked one contact and scroll down list view and i am back to scroll up the checked box automatically unchecked

here my contact picker activity :

if (cb.isChecked()) {
                        Log.i("in if check box", receiver_id+"++++");

                        obj_Send_data = new send_data();
                        obj_Send_data.setReceiver_id(receiver_id);
                        obj_Send_data.setGcm_reg_id(gcm_reg_id);
                        id_arrArrayList.add(obj_Send_data);
                        Log.i("arraylist in if lisze", "++"+ id_arrArrayList.size());

                        Log.i("array list obj","++"+obj_Send_data);

                    } else if (!cb.isChecked()) {

                        id_arrArrayList.remove(obj_Send_data);
                        Log.i("arraylist in else lisze", "++"+ id_arrArrayList.size());

                    }


================================================================================================
here my custom listview adapter
================================================================================================


public class CustomListViewAdapter extends BaseAdapter {

    Context cm1;
    ArrayList<PhoneContactInfo> itemdetails,arraylist;
    LayoutInflater l_inflater;
    String[] str;
    ArrayList<String> image1;
    int[] intarray_;
    boolean[] itemChecked;
      /* List<Registure_user> registure_user_list;*/


 private static class ViewHolder {
     public ImageView imageView;
     public TextView name,number;
     CheckBox ch;

 }
    public CustomListViewAdapter(Context context,ArrayList<PhoneContactInfo> list,List<Registure_user> registure_user,ArrayList<String> image) {
        cm1=context;
        itemdetails=list;
        l_inflater=LayoutInflater.from(context);
        image1=image;
//      registure_user_list=registure_user;
        arraylist=new ArrayList<PhoneContactInfo>();
        arraylist.addAll(list);
        itemChecked = new boolean[arraylist.size()];
    }


    public int getCount() {
        return itemdetails.size();
    }

    public Object getItem(int arg0) {
        return itemdetails.get(arg0);
    }

    public long getItemId(int arg0) {
        return arg0;
    }

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

        final ViewHolder holder;
        View vi = convertView;

        if(vi==null)
            vi = l_inflater.inflate(R.layout.custcontactview, null);

            holder = new ViewHolder();

            holder.number = (TextView) vi.findViewById(R.id.txtContactNumber);
            holder.name= (TextView) vi.findViewById(R.id.txtContactName);
            holder.imageView = (ImageView) vi.findViewById(R.id.imageView1_icon);
            holder.ch=(CheckBox) vi.findViewById(R.id.checkBox1_check);
            holder.ch.setClickable(false);


            holder.name.setText(itemdetails.get(position).getcontactName());
            holder.number.setText(itemdetails.get(position).getcontactNumber());

            if(!image1.get(position).equals("0")){
                holder.imageView.setVisibility(View.VISIBLE);
                holder.ch.setVisibility(View.VISIBLE);
                vi.setTag(image1.get(position)+" ");


                holder.ch.setTag(vi.getTag());
                Log.i("tag", vi.getTag()+"==");
            } 
            else {
                holder.imageView.setVisibility(View.INVISIBLE);
                holder.ch.setVisibility(View.INVISIBLE);
                vi.setTag("0");
            }

            return vi;
    }
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        itemdetails.clear();
        if (charText.length() == 0) {
            itemdetails.addAll(arraylist);
        } else {
            for (int i=0;i<arraylist.size();i++ ) {
                if (arraylist.get(i).getcontactName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    Log.i("serch", arraylist.get(i).getcontactName());
                    itemdetails.addAll(arraylist);
                }
            }
        }
        notifyDataSetChanged();
    }
}

Its getview() method behavior which create new view every time whenever user scroll the view. Check my post at my blog simple replace toggle button with checkbok and execute it.

Getview Method Wierd in Listview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM