Android基于复选框更新sqlite中的值
我在sqlite表中有一个值,它会根据复选框的状态进行更新。 目前,该值确实会更新,但只有在我关闭应用程序时才会更新。 我想要的是:Android基于复选框更新sqlite中的值,android,sqlite,checkbox,Android,Sqlite,Checkbox,我在sqlite表中有一个值,它会根据复选框的状态进行更新。 目前,该值确实会更新,但只有在我关闭应用程序时才会更新。 我想要的是: 复选框 更新sqlite表中的值 更新/刷新视图以立即显示更新的值 另外,我将复选框侦听器放在适配器中。这样行吗?如果没有,我如何在主活动上使用侦听器 提前感谢你的帮助 下面是一些代码 适配器: public View getChildView(final int groupPosition, final int childPosition, boolean i
另外,我将复选框侦听器放在适配器中。这样行吗?如果没有,我如何在主活动上使用侦听器 提前感谢你的帮助
下面是一些代码 适配器:
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
db = new MyDatabase(context);
final Child child = (Child) getChild(groupPosition,childPosition);
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout,null);
}
convertView.setTag(getChild(groupPosition,childPosition).toString());
TextView value = (TextView) convertView.findViewById(R.id.value);
value.setText(String.valueOf(child.getValue()));
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);
if(child.getValue() == 0)
{
cb.setChecked(true);
}
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true)
{
String id = String.valueOf(childPosition+1);
db.updateIsChecked(id);
}
else
{
String id = String.valueOf(childPosition+1);
db.updateIsNotChecked(id);
}
}
});
return convertView;
}
数据库:
public void updateIsChecked(String id)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues newValues = new ContentValues();
newValues.put("value", 0);
db.update("table",newValues,"id =? ",new String[] {id});
db.close();
}
您的所有代码似乎都很好。您需要做的是尝试以下代码
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b)
{
String id = String.valueOf(childPosition+1);
db.updateIsChecked(id);
}
else
{
String id = String.valueOf(childPosition+1);
db.updateIsNotChecked(id);
}
notifyDataSetChanged();
}
});
编辑
有一个很好的博客可以处理列表中的“复选框”视图以保持其状态。请看。我还使用此代码修复复选框状态问题
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
您只需要调用
notifyDataSetChanged()使用适配器中的code>更新/刷新列表视图
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
String id = String.valueOf(childPosition+1);
db.updateIsChecked(id);
} else {
String id = String.valueOf(childPosition+1);
db.updateIsNotChecked(id);
}
notifyDataSetChanged();
}
});
我正在将复选框侦听器放入适配器中。这样行吗?>>是的,它是OKAYCalladapter.notifyDataSetChanged()。我确实使用了notifyDataSetChanged(),但该值只有在重新打开应用程序后才会更新。对于if语句(if值为0,选中),我无法取消选中该框,因为它只是再次检查自身。感谢您的帮助,但我仍然没有得到我需要的。我已经将代码上传到github——它可能会提供更好的关于我哪里出错的信息。我已经应用了从您提供的链接中收集的内容,但它仍然存在相同的问题:sqlite数据库仅在应用程序重新启动时更新,这导致复选框出现问题。我确实使用了notifyDataSetChanged(),但值仅在我重新打开应用程序后更新。对于if语句(if值为0,选中),我无法取消选中该框,因为它只是再次检查自身。我应该显示代码的哪一部分?我应该复制粘贴整个适配器吗?您认为相关的部分@Az37