【问题标题】:Android Updating value in sqlite based on CheckboxAndroid基于Checkbox更新sqlite中的值
【发布时间】:2014-12-24 18:12:48
【问题描述】:

我在一个 sqlite 表中有一个值,该值根据复选框的状态进行更新。 目前,该值确实会更新,但仅在我关闭应用程序时才会更新。 我想要的是:

  1. 勾选复选框
  2. 更新sqlite表中的值
  3. 更新/刷新视图以立即显示更新的值

另外,我将复选框侦听器放入适配器中。这个可以吗?如果没有,我该如何在主要活动上使用监听器?

提前感谢您的帮助。


这里是一些代码

适配器:

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

【问题讨论】:

  • 我将复选框监听器放在适配器中。这个可以吗? >>> 是的,没关系
  • 在 setOnCheckedChangeListener 中调用 adapter.notifyDataSetChanged();

标签: android sqlite checkbox


【解决方案1】:

您的所有代码似乎都很好。您需要做的是尝试以下代码。

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

编辑

有一个很好的博客来处理列表中的“复选框”视图以保持其状态。请看ListView with CheckBox Scrolling Issue。我还使用此代码来修复复选框状态问题。

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(),但只有在我重新打开应用程序后才会更新值。使用 if 语句(如果值为 0,选中),我无法取消选中该框,因为它只是再次检查自己。
  • 感谢您的帮助,但我仍然没有得到我需要的东西。我已将代码上传到 github - github.com/Az35/test - 它可能会在我出错的地方提供更好的信息。我已经应用了从您提供的链接中收集的内容,但仍然存在同样的问题:sqlite 数据库仅在应用程序重新启动时更新,这会导致复选框出现问题。
【解决方案2】:

您只需在您的adapter 中调用notifyDataSetChanged(); 即可更新/刷新ListView。

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

【讨论】:

  • 我确实使用了 notifyDataSetChanged(),但只有在我重新打开应用程序后才会更新值。使用 if 语句(如果值为 0,请检查),我无法取消选中该框,因为它只是再次检查自己。
  • 我应该显示代码的哪一部分?我应该复制粘贴整个适配器吗?
  • 您认为相关的部分。 @Az37
猜你喜欢
  • 2012-04-28
  • 2014-10-05
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
相关资源
最近更新 更多