2014-10-29 58 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