简体   繁体   English

如何与广播组一起使用共享首选项

[英]how to use shared preferences with radio group

I have two buttons in next and previous , question textview and their answers option in Radio button . 下一个和上一个我有两个按钮,问题文本视图和单选按钮中的答案选项。 I have to save the radio buttons values in Shared Preferences on click Next button and when i call previous button it gets the Shared Preferences value from next button and set it into radio button when i call the previous button.But after run the application radio button value saves in shared preferences.But When i call previous button does not get the Shared Preferences value and does not show the Radio button is checked. 我必须在单击下一步按钮时将单选按钮值保存在共享首选项中,当我调用上一个按钮时,它会从下一个按钮获取共享首选项值,并在我调用上一个按钮时将其设置为单选按钮。但是在运行应用程序单选按钮后值保存在共享的首选项中。但是当我呼叫上一个按钮时,不会获得“共享的首选项”值,并且不显示单选按钮已选中。 Can some one help me here.Thanks in Advanced. 有人可以帮到我吗?谢谢进阶。

Here is my Activity code. 这是我的活动代码。

 read_Questions_XML_File();

          button_Previouse.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    previousQuestionCalled(v);
                }
            });


            button_Next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    nextQuestionCalled(v);
                }
            });


public void nextQuestionCalled(View view)
    {

        int id = Options_RadioGroup.getCheckedRadioButtonId();
        System.out.println("id = " + id);
        if (id > 0)
        {   

            Options_RadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup R_Group, int checked_ID) {
                    // TODO Auto-generated method stub

                    RadioButton radioButton = (RadioButton)R_Group. findViewById(checked_ID);
                    int checkedIndex = R_Group.indexOfChild(radioButton);
                    System.out.println("checkedIndex = " + checkedIndex);

                    SharedPreferences sharedPreferences = getSharedPreferences("RGROUP_SHARED_PREF", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("check", checkedIndex);
                    editor.commit(); 


                }
            });


            //if animationForward == false
            if (animationForward)
            {
                storingResult();
                boolean flag;
                if (animationForward)
                {  flag = false; }

                else 
                { flag = true; }

                animationForward = flag;
            }

        //Return The Number Of Element in this Vector > 0
        //-1 + 1
        if (-1 + Vectore_mquestionDatabaseStructure.size() > StaticClass.QuestionNumber)
        {
            if (StaticClass.isTest) 
            {
                StaticClass.resultOfTest_Out_OF = 1 + StaticClass.resultOfTest_Out_OF;
            }

            StaticClass.QuestionNumber = 1 + StaticClass.QuestionNumber;
            reHitting();
            view.clearAnimation();

            if (!StaticClass.isTest) {
                button_QuestionLimit.clearAnimation();
            }

            if (StaticClass.isTest) {
                button_QuestionLimit.clearAnimation();
            }

            return;
        }

        else
        {
            button_QuestionLimit.startAnimation(mAnimation);
            return;
        }
    }

        else if (id == -1)
        {
            Toast.makeText(this, "Please Select Any Option", Toast.LENGTH_LONG)
                    .show();
        }
    }

 public void previousQuestionCalled(View view)
    {
         System.out.println("_________________in Previous Question____________");
         SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
         int savedRadioIndex = sharedPreferences.getInt("check", 0);
         System.out.println("savedRadioIndex is "+savedRadioIndex);
         RadioButton savedCheckedRadioButton = (RadioButton)Options_RadioGroup.getChildAt(savedRadioIndex);
         savedCheckedRadioButton.setChecked(true);
         System.out.println("_________________in Previous Question____________");

        if (!animationForward)
        {
            boolean flag;

            if (animationForward) 
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            animationForward = flag;
        }
        if (StaticClass.QuestionNumber > 0)
        {
            if (!StaticClass.isTest)
            {
                StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
                reHitting();
                button_QuestionLimit.clearAnimation();
            }
            view.clearAnimation();


        } 
        else 
        {
            button_QuestionLimit.startAnimation(mAnimation);
        }

    }

When i press Next button saved the Shared Prefs Value in = int checkedIndex = R_Group.indexOfChild(radioButton); 当我按下“下一步”按钮时,将共享的Prefs值保存在= int CheckedIndex = R_Group.indexOfChild(radioButton);中。

But when i press Previous button here I'm getting Shared Prefs Value is zero or -1 and Radio button is unchecked. 但是,当我在这里按“上一步”按钮时,我得到的“共享首选项”值为零或-1,并且未选中“单选”按钮。

Here is my Logcat info 这是我的Logcat信息

10-08 16:47:54.007: I/System.out(459): id = 2131165205
10-08 16:47:54.007: I/System.out(459): strLimit = 2/12
10-08 16:47:54.007: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.016: I/System.out(459): checkedIndex = 2
10-08 16:47:54.086: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.086: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.086: I/System.out(459): checkedIndex = -1
10-08 16:47:54.096: I/System.out(459): ************************IN Next Button******************
10-08 16:48:06.756: I/System.out(459): _________________in Previous Question____________
10-08 16:48:06.756: I/System.out(459): savedRadioIndex is -1

After that i have checked xml file from DDMS its not store the value n that .It also store the -1 value .And here log cat shows the shared prefs value. 之后,我从DDMS检查了xml文件,它没有存储值n。它还存储了-1值。这里的log cat显示了共享的prefs值。

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="check" value="-1" />
</map>

In RadioGroup, you have to create localInstance while you are trying to save to get the current selected choice in RadioGroup. 在RadioGroup中,当您尝试保存以获取RadioGroup中的当前选定选项时,必须创建localInstance。 I have created simplest example and You can implement as mentioned below : 我创建了最简单的示例,您可以按以下说明实施:

private void saveRadioChoice(){
SharedPreferences mSharedPref = getSharedPreferences(MY_PREF_KEY,MODE_PRIVATE);

SharedPreferences.Editor editor = mSharedPref.edit();

// Initialize Radiogroup while saving choices
RadioGroup localRadioGroup = (RadioGroup) findViewById(R.id.choices);
editor.putInt(my_choice_key, localRadioGroup.indexOfChild(findViewById(localRadioGroup.getCheckedRadioButtonId())));
editor.apply();
}

after saving this value, you can retrieve it as follows 保存此值后,可以按以下方式检索它

private void retrieveChoices(){

SharedPreferences sharedPref = getSharedPreferences(MY_PREF_KEY,MODE_PRIVATE);
int i = sharedPref.getInt(my_choice_key,-1);
if( i >= 0){
((RadioButton) ((RadioGroup)findViewById(R.id.myChoiceViewRadio)).getChildAt(i)).setChecked(true);
}


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM