Anuj
Anuj

Reputation: 398

List view not getting refreshed

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;

    }
}

Upvotes: 0

Views: 106

Answers (2)

Jezer Crespo
Jezer Crespo

Reputation: 2172

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

Upvotes: 0

DjP
DjP

Reputation: 4577

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()

Upvotes: 1

Related Questions