选中的复选框未插入数据库

时间:2014-08-11 09:39:28

标签: android checkbox android-sqlite

我有一个带有复选框的联系人列表。每当用户检查其中的特定值时,都会调用更新语句,但在我的情况下,没有任何内容被更新???为什么会发生这种情况

代码

public class GroupAddContactCustomAdapter extends BaseAdapter {

    private Context context;
    ArrayList<ContactModel> contactList;
    DbHandler dbHandler;

    public GroupAddContactCustomAdapter(Context context, ArrayList<ContactModel> contactList) {
        this.context = context;
        this.contactList = contactList;

    }


    @Override
    public int getCount() {
        return contactList.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;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.group_custom_contact_list, viewGroup, false);
            holder.tvContactName = (TextView) view.findViewById(R.id.tv_group_contact_name);
            holder.cbContacts = (CheckBox) view.findViewById(R.id.cb_contacts);
            view.setTag(holder);
            holder.cbContacts.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CheckBox checkBox = (CheckBox) view;
                    ContactModel contactModel = (ContactModel) checkBox.getTag();
                    if (checkBox.isSelected()) {
                        dbHandler = new DbHandler(context);
                        dbHandler.updateContactList(contactList.get(i).getContactName(), 1);
                    }
                }
            });
        } else {
            holder = (ViewHolder) view.getTag();
        }


        holder.tvContactName.setText(contactList.get(i).getContactName());


        return view;
    }

    private class ViewHolder {
        private TextView tvContactName;
        private CheckBox cbContacts;

    }
}

在这一行,它没有进入if条件

if (checkBox.isSelected()) {
                            dbHandler = new DbHandler(context);
                            dbHandler.updateContactList(contactList.get(i).getContactName(), 1);
                        }

我试过

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

                if (holder.cbContacts.isChecked()) {
                    dbHandler.updateContactList(contactList.get(i).getContactName(), 1);
                }

            }
        });

但是收到此错误

08-11 15:44:50.900  32075-32075/example.com.pocketdocs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at example.com.pocketdocs.Group.GroupAddContactCustomAdapter$1.onCheckedChanged(GroupAddContactCustomAdapter.java:68)
            at android.widget.CompoundButton.setChecked(CompoundButton.java:130)
            at android.widget.CompoundButton.toggle(CompoundButton.java:91)
            at android.widget.CompoundButton.performClick(CompoundButton.java:103)
            at android.view.View$PerformClick.run(View.java:17161)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

I think there is problem with your on-click listner

    public class GroupAddContactCustomAdapter extends BaseAdapter {

        private Context context;
        ArrayList<ContactModel> contactList;
        DbHandler dbHandler;

        public GroupAddContactCustomAdapter(Context context, ArrayList<ContactModel> contactList) {
            this.context = context;
            this.contactList = contactList;

        }


        @Override
        public int getCount() {
            return contactList.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;

            if (view == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.group_custom_contact_list, viewGroup, false);
                holder.tvContactName = (TextView) view.findViewById(R.id.tv_group_contact_name);
                holder.cbContacts = (CheckBox) view.findViewById(R.id.cb_contacts);
                view.setTag(holder);

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


            holder.tvContactName.setText(contactList.get(i).getContactName());

     holder.cbContacts.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
                        ContactModel contactModel = (ContactModel) checkBox.getTag();
                            if (checkBox.isSelected()) {
                            dbHandler = new DbHandler(context);
                            dbHandler.updateContactList(contactList.get(i).getContactName(), 1);
                        }
                    }
                });
            return view;
        }

        private class ViewHolder {
            private TextView tvContactName;
            private CheckBox cbContacts;

        }
    }

在返回视图之前添加侦听器。这可能是工作

您可以使用

holder.cbContacts.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    // your code here
                }
            }
        });

而不是点击列表器。