2014-10-29 22 views
0

我在一个sqlite表中有一个值,它根据复选框的状态进行更新。 目前,该值不会更新,但只有当我关闭应用程序。 我要的是:Android在基于复选框的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(); 
} 
+0

我把复选框侦听器放在适配器中。这个可以吗? >>> **是的OKAY ** – 2014-10-29 05:56:02

+2

在setOnCheckedChangeListener中调用'adapter.notifyDataSetChanged();'。 – 2014-10-29 05:58:48

回答

0

你所有的代码似乎罚款。你需要做的是尝试下面的代码。

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. 
    } 
}); 
+0

我确实使用了notifyDataSetChanged(),但只有在我重新打开应用程序后才更新该值。用if语句(如果值为0,选中),我不能取消选中该框,因为它只是再次检查自己。 – Az37 2014-10-29 06:18:26

+0

@ Az37好吧..得到它..等..给你一个很好的解决方案.. – 2014-10-29 06:57:20

+0

@ Az37我编辑了我的答案。请检查。 – 2014-10-29 08:19:22

0

你只需要调用notifyDataSetChanged();adapter更新/刷新的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(); 
    } 
}); 
+0

我确实使用了notifyDataSetChanged(),但只有在我重新打开应用程序后才会更新该值。使用if语句(如果值为0,则选中),我不能取消选中该框,因为它只是再次检查自己。 – Az37 2014-10-29 06:21:46

+0

显示您的代码@ user2639969 – 2014-10-29 06:28:10

+0

我应该显示哪部分代码?我应该复制粘贴整个适配器? – Az37 2014-10-29 06:38:20