Android 选中的复选框未插入数据库
我有一个带有复选框的联系人列表。每当用户检查其中的某个特定值时,就会调用一个update语句,但在我的情况下,没有任何内容得到更新???为什么会发生这种情况 代码 我试过了Android 选中的复选框未插入数据库,android,checkbox,android-sqlite,Android,Checkbox,Android Sqlite,我有一个带有复选框的联系人列表。每当用户检查其中的某个特定值时,就会调用一个update语句,但在我的情况下,没有任何内容得到更新???为什么会发生这种情况 代码 我试过了 holder.cbContacts.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(Compound
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)
您可以在@user3917131选中类似的答案,而不是单击listner。U我想要的是,当用户选中特定复选框时,该值应更新为1选中更新的答案。或者,如果您遇到相同的问题,则发布所有代码。您在clickListner中初始化了复选框。在外部初始化。这就是为什么没有调用您的事件。请参考此链接Contacts.setOnCheckedChangeListenernew CompoundButton.OnCheckedChangeListener{@Override public void onCheckedChangedCompoundButton CompoundButton,布尔b{如果b{dbHandler.updateContactListcontactList.geti.getContactName,1;}}};为什么要添加holder.cbContacts.isChecked
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)
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
}
}
});