存储多个无线电组状态 android

store multiple radio groups status android

我正在开发一个有多个无线电组的应用程序。
我想保存他们的检查状态并在下次重启时恢复它。

那么哪种方法最有效?

我认为您需要使用 SharedPreferences 将选中的项目存储在单选组中,因为除非我们卸载应用程序,否则它们将存在。

在共享首选项中存储值

List<RadioGroup> radioGroups;
List<String> savedids;

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("number of radio groups", radiogroupsCount);
for(int i=0; i< radiogroupsCount; i++){
editor.putString("radioGroup"+String.valueOf(i), radioButtonGroup.getCheckedRadioButtonId(););
}
editor.commit();();

要从共享首选项中检索值:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
 String count = preferences.getString("number of radio groups", 0);
if(count >=0){
for(int i=0; i < count; i++){
 savedIds.add(preferences.getString("radioGroup"+String.valueOf(i),""));
  }
 }