0

I have displayed all the contacts within the phone along with a check box in a list view. Now when the user checks say A and B and clicks on "ok" button, then what I want is to display the list again when making all the check box checked value to false. For that, I have created a method but when I call this method the value of the selected contacts is set to unchecked only when the list is scrolled, else it remains unchecked. Whats the problem with my code???

Code

public void getContactSync(Context context, ArrayList<ContactModel> data) {
        setListAdapter(null);
        contactListAdapter = new ContactListAdapter(context, data);
        setListAdapter(contactListAdapter);
//        contactListAdapter.notifyDataSetChanged();
    }

On OK button click

Arrays.fill(ContactListAdapter.contacts, 0);
                            contactListFragment.getContactSync(getActivity(), dbHandler.getAlGetContacts());

Custom Adapter

public class ContactListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ContactModel> data;
    DbHandler dbHandler;
    public static int[] contacts;
    static ArrayList<String> contactsSepetrated;
    public static ArrayList<String> contactsId;

    public ContactListAdapter(Context context, ArrayList<ContactModel> data) {
        this.context = context;
        this.data = data;
        contacts = new int[data.size()];
        contactsSepetrated = new ArrayList<String>();
        contactsId = new ArrayList<String>();
    }

    @Override

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

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;
        dbHandler = new DbHandler(context);

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.contact_custom_list, viewGroup, false);
            holder.tvContact = (TextView) view.findViewById(R.id.tv_contact_name);
            holder.checkBox = (CheckBox) view.findViewById(R.id.cb_contact_checkbox);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                                       @Override
                                                       public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                                                           if (compoundButton == holder.checkBox) {
                                                               if (b) {
                                                                   contacts[i] = 1;
                                                                   //dbHandler.updateContactList(data.get(i).getUserID(), 1);


//

                                                               } else {

                                                                   contacts[i] = 0;
                                                               }
                                                           }

                                                       }
                                                   }

        );

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (contacts[i] == 1) {

                    contactsSepetrated.add(data.get(i).getContactName());
                    Log.e("Contact values", contactsSepetrated.toString());
                    contactsId.add(data.get(i).getUserID());
                    Log.e("Position", "" + i);


                } else if (contacts[i] == 0) {

                    contactsSepetrated.remove(data.get(i).getContactName());
                    contactsId.remove(data.get(i).getUserID());
                    Log.e("Contact values", contactsSepetrated.toString());
                    Log.e("Position", "" + i);
                }

                ShareWithinpocketDocs.etContactsList.setText(contactsSepetrated.toString().subSequence(1, contactsSepetrated.toString().length() - 1));


            }
        });


        if (contacts[i] == 0) {
            holder.checkBox.setChecked(false);
//            emailSeperated.remove(data.get(i).getEmail());
//            Log.e("Email values", emailSeperated.toString());

//            ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
        } else {
            holder.checkBox.setChecked(true);
//            emailSeperated.add(data.get(i).getEmail());
//            Log.e("Email values", emailSeperated.toString());

        }


        holder.tvContact.setText(data.get(i).getContactName());


        return view;
    }

    private class ViewHolder {
        TextView tvContact;
        CheckBox checkBox;

    }
}
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
Anuj
  • 402
  • 8
  • 16
  • I think you have read notifyDataSetChanged() here :http://developer.android.com/reference/android/widget/BaseAdapter.html – Haresh Chhelana Sep 15 '14 at 06:25
  • possible duplicate of [How to refresh Android listview?](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview) – Kick Buttowski Sep 15 '14 at 06:51

2 Answers2

1

on click of checkbox inside adapter just call notifydatasetchanged() it will solve your problem

holder.checkBox
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton,
                        boolean b) {

                    if (compoundButton == holder.checkBox) {
                        if (b) {
                            contacts[i] = 1;
                            // dbHandler.updateContactList(data.get(i).getUserID(),
                            // 1);

                            //
                            notifyDataSetChanged();

                        } else {

                            contacts[i] = 0;
                            notifyDataSetChanged();
                        }
                    }

                }
            }

            );

if you want to refresh adapter on ok button click add this to ok button click

adapter.notifydatasetchagned()
DjP
  • 4,537
  • 2
  • 25
  • 34
  • Nah not working,i tried but the selected contact only gets unselected when i scroll the list – Anuj Sep 15 '14 at 06:30
  • there is no need of checkbox set on click listener you can do that same in checkbox checkchangedListener – DjP Sep 15 '14 at 06:41
0

Try to call setListAdapter(contactListAdapter); again inside your onClick()

WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
Jezer Crespo
  • 2,152
  • 3
  • 24
  • 27