2012-02-18 111 views
0

我有一个与复选框自定义列表视图。我希望在应用程序的整个生命周期内保存复选框的状态。我怎样才能做到这一点?谢谢如何保存列表视图中复选框的状态永远存在?

+0

你这样的问题不能回答。请提供更多信息。比如你显示的数据来自哪里。如果它的一个sqlite数据库然后将状态存储在你的数据库中。如果您的数据是从服务器分析的... – KarlKarlsom 2012-02-18 09:41:05

+0

复选框由用户检查,它们不受任何其他服务的影响。 – Dinesh 2012-02-18 09:47:00

回答

2

SharedPreferences类提供了一个总体框架,使您可以保存和检索的原始数据类型的持久键 - 值对。

为了完整起见,我包括示例代码在这里。

public class Xxx extends Activity { 
public static final String PREFS_NAME = "MyPrefsFile"; 

@Override 
protected void onCreate(Bundle state){ 
    super.onCreate(state); 
    . . . 

    // Restore preferences 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    boolean silent = settings.getBoolean("silentMode", false); 
    setSilent(silent); 
} 

@Override 
protected void onStop(){ 
    super.onStop(); 

    // We need an Editor object to make preference changes. 
    // All objects are from android.context.Context 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putBoolean("silentMode", mSilentMode); 

    // Commit the edits! 
    editor.commit(); 
} 

}

+0

我知道,但我有我的listview约16复选框,我想保存每个复选框的状态。 – Dinesh 2012-02-18 09:44:56

+0

我认为那么[Preference Activity](http://developer.android.com/reference/android/preference/Preference.html)是你正在寻找的东西。这[教程](http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html)应该让你加快速度! – 2012-02-18 09:47:07

+0

我有一个布尔数组存储每个复选框的状态。问题是这个数组不是持久的。但sharedprefrences不支持数组。 – Dinesh 2012-02-18 09:54:05

0

ListView中你不应该使用复选框,我更喜欢使用TableLayout他们。 但是,如果你想使用ListView控件,请考虑以下前面的问题: -

Android save Checkbox State in ListView with Cursor Adapter

Finding the Checked state of checkbox in a custom listview

Code does not extend ListViewActivity, but does have a listview

海有很多关于同一主题的其他问题,可以考虑为他们寻找太。

编辑:示例动态TableLayout取代的ListView

XML: -

<ScrollView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TableLayout 
android:id="@+id/table" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
</TableLayout> 
</ScrollView> 

代码片段在Java中: -

TableLayout table=(TableLayout)findViewByid(R.id.table); 
ArrayList<CheckBox> cbData=new ArrayList<CheckBox>(); 
for(int i=0;i<rowCount;i++){ 
    TextView t1=new TextView(this); 
    t1.setText("Text1"); 
    TextView t2=new TextView(this); 
    t2.setText("Text2"); 
    CheckBox cb=new CheckBox(this); 
    TableRow row=new TableRow(this); 
    row.addView(t1); 
    row.addView(t2); 
    row.addView(cb); 
    cbData.add(cb); 
    table.addView(row); 
} 

使用上面的代码,你可以添加与TableLayout中想要的行一样多,并且由于TableLayout不会回收它的视图,所以您不必缓存复选框数据a nd状态。要访问复选框,可以使用cbData.get(index)方法。比缓存复选框状态编码更容易。

+0

我的问题是不同的,我希望复选框的状态被保留。 – Dinesh 2012-02-18 09:49:21

+0

然后在ScrollView中使用TableLayout或线性布局将是最简单的选择。如果你想继续使用ListView,一旦你从复选框获得数据,你必须让方法更新onFling或onScroll Listener中的复选框。但让我告诉你,这是不值得的努力。 – noob 2012-02-18 10:02:04

+0

我有一个listview成功地与复选框一起工作,但问题是我想复选框的状态是应用程序持久化,而不是活动持久化。 – Dinesh 2012-02-18 10:12:52