我在一个sqlite表中有一个值,它根据复选框的状态进行更新。 目前,该值不会更新,但只有当我关闭应用程序。 我要的是:Android在基于复选框的sqlite中更新值
- 检查复选框
- 更新的SQLite表
- 更新/刷新视图值,显示更新后的值立即
另外,我也把复选框监听器在适配器中。这个可以吗?如果不是,我如何在主要活动中使用监听器?
在此先感谢您的帮助。
下面是一些代码
适配器:
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();
}
我把复选框侦听器放在适配器中。这个可以吗? >>> **是的OKAY ** – 2014-10-29 05:56:02
在setOnCheckedChangeListener中调用'adapter.notifyDataSetChanged();'。 – 2014-10-29 05:58:48