2013-07-29 40 views
2

我有一個有listview的應用程序。列表視圖中的每個視圖都具有自定義xml並具有按鈕複選框和文本視圖。我必須從sqlite數據庫中獲取以前保存的複選框設置並設置它們。如果用戶更改複選框或單選按鈕,我必須將這些設置保存在數據庫中。在列表視圖中更新信息的設計

嘗試了幾種不同的方式來做到這一點,大多數導致錯誤,並且在返回頁面時不會顯示覆選框的正確設置。

終於工作的是以下所示的設計。在那裏我把任何數據庫調用盡可能靠近實際視圖。然而,它看起來像是在列表視圖中有很多行,它會對數據庫進行多次調用,並且可能會變慢。

是我設計的正確或是否有更好的辦法

psudocode

onCreate 


instantiate arraylist adapter and set adapter to view 

end onCreate 

arrayadapter class 

getView(){ 

call databsase and get previous settings for checkboxes in this view and 
set the checkboxes to show checked or not depending on database saved settings 


onClicklistener or oncheckedchanged listener for getting checkbox positions if changed 

call database and set method to store updated positions of checkbox, radiobutton or spinner 


} 


end arrayadapter 

回答

0

你的做法是正確的,你的擔憂是有道理的。我可以建議你下面的方法,但它的性能會有進行測試:

onCreate 

    instantiate arraylist adapter and set adapter to view 

end onCreate 

arrayadapter class 

    getView(){ 

     call databsase and get previous settings for checkboxes in this view and 
     set the checkboxes to show checked or not depending on database saved settings 

     ----> Create two ArrayLists: ArrayList<Boolean> for CheckBoxes 
     ----> and ArrayList<Integer> for RadioButtons. 
     ----> Fill these lists using the values from the databse 

     onClicklistener or oncheckedchanged listener for getting checkbox positions if changed 

     call database and set method to store updated positions of checkbox, radiobutton or spinner 

     ----> Here, update the lists, not the database. 
     ----> Execute a AsyncTask to update the database whenever the lists change. 

    } 


end arrayadapter 
0

另一種認爲你能做的就是在Activity.onStart(從數據庫中裝載的數據),並保存它放回活動的onPause() 。所有其他時間使用ArrayList來跟蹤UI中的更改。

在這種情況下,您將在活動生命週期中很少有這種沉重的數據庫操作。