如何从共享首选项存储和访问单选按钮? [英] How to store and access Radio Buttons from Shared Preferences?

查看:42
本文介绍了如何从共享首选项存储和访问单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在共享首选项中保存单选按钮状态有很多问题,但没有一个提供我正在寻找的解决方案.

There are a lot of questions on SO about saving radio buttons state in Shared Preferences but none of them provide the solution that I'm looking for.

我在 for 循环中的每个单选组内创建了 15 个单选组和 4 个单选按钮.单选组的唯一 ID 为 1 到 15,单选按钮的 ID 范围为 100 到 159.

I'm creating 15 radio groups and 4 radio buttons inside each radio group in a for loop. The radio groups have unique id from 1 to 15 and the radio buttons have ids ranging from 100 to 159.

这是我用来保存单选按钮 ID 的代码:

This is the code I use to save radio buttons id:

public void saveArray(int[] array, String arrayName) {
    SharedPreferences prefs = getActivity().getSharedPreferences("preferencename", 0);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(arrayName +"_size", array.length);
    for(int i=0;i<array.length;i++) {
        editor.putInt(arrayName + "_" + i, array[i]);
    }
    editor.commit();
}

array 包含所有被选中的单选按钮的唯一 ID,我已经测试了上面的代码,它运行良好,但问题是我无法访问它.

array contains the unique ID of all those radio buttons that are checked and I've tested the above code, it works perfectly but the problem is I can't access it.

这是我用来检索单选按钮 ID 的代码:

This is the code I use to retrieve radio button id:

public void loadArray(String arrayName, Context mContext) {
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
    int size = prefs.getInt(arrayName + "_size", 0);
    int array[] = new int[size];
    for(int i=0;i<size;i++) {
        array[i] = prefs.getInt(arrayName + "_" + i, 0);

    }
}

saveArray 一样,loadArray 也能工作.该数组由正确的单选按钮 ID 填充.问题现在开始 - 我无法访问那些单选按钮,我无法执行 findViewById() 因为这仅在 onCreateView()

Like saveArray, loadArray also works. The array is populated by the correct radio button ids. The problem starts now - I can't access those radio buttons, I can't do findViewById() because that is only available in onCreateView()

所以这在 loadArray

    for(int i=0; i<size; i++) {
        RadioButton radio1 = (RadioButton) findViewById(array[i]);
    }

findViewById 的可能解决方案是将视图存储在 onCreateView() 中,然后稍后访问它.所以我创建了一个单选按钮数组

The possible solution to findViewById is storing the views in onCreateView() and then accessing it later. So I created a Radio Button array

RadioButton[] allRadio = new RadioButton[15];

现在在 onCheckedChanged 中,我将选定的单选按钮添加到数组中:

Now inside onCheckedChanged, I'm adding the selected radio buttons to the array:

public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton radio = (RadioButton) rootView.findViewById(i);
        String selectedValue = radio.getText().toString();
        allRadio[radioGroup.getId()-1] = radio;
}

上面的代码完美地将单选按钮存储在数组中,但现在我无法保存它们!您可以在共享首选项中存储 int、string 等,但不能在单选按钮中存储!对此的解决方案可能是存储单选按钮 id,但同样的问题将重新开始,findViewById() 不可用.

The above code perfectly stores the radio buttons inside the array but now I can't save them! You can store int, string etc.. in Shared Preferences but not radio buttons! The solution to this could be to store radio button id but then the same problem will start all over again that findViewById() is not available.

推荐答案

public void loadArray(String arrayName, Context mContext) {
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
    int size = prefs.getInt(arrayName + "_size", 0);
    int array[] = new int[size];
    for(int i=0;i<size;i++) {
        array[i] = prefs.getInt(arrayName + "_" + i, 0);
//->put Toast here and check whether your are getting the id's or not. If you
//->are getting it, return arrays of id back to calling method
    }
}

这篇关于如何从共享首选项存储和访问单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆