列表视图未刷新

时间:2014-09-15 06:19:10

标签: android listview

我已在电话中显示了所有联系人以及列表视图中的复选框。现在,当用户检查说出A和B并点击" ok"按钮,然后我想要的是在将所有复选框选中的值设置为false时再次显示列表。为此,我创建了一个方法,但是当我调用此方法时,所选联系人的值仅在滚动列表时设置为取消选中,否则保持未选中状态。 我的代码问题是什么?

代码

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

在“确定”按钮上单击

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

自定义适配器

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;

    }
}

2 个答案:

答案 0 :(得分:1)

点击适配器内的复选框只需调用notifydatasetchanged()它就可以解决你的问题

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

                }
            }

            );

如果要在ok按钮上刷新适配器,请单击将此按钮添加到ok按钮单击

adapter.notifydatasetchagned()

答案 1 :(得分:0)

尝试致电setListAdapter(contactListAdapter);再次进入onClick()