如何将共享首选项与无线电组一起使用

时间:2014-10-08 10:52:04

标签: java android

我在下一个和上一个中有两个按钮,问题textview和他们在单选按钮中的答案选项。我必须在共享首选项中单击下一步按钮保存单选按钮值,当我调用上一个按钮时,它从下一个按钮获取共享首选项值,并在我调用上一个按钮时将其设置为单选按钮。但是在运行应用程序单选按钮后值保存在共享首选项中。但是当我调用上一个按钮时没有获得共享首选项值并且没有显示单选按钮被选中。有人可以帮助我。感谢高级。

这是我的活动代码。

 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);
        }

    }

当我按下Next按钮时,保存了共享首选项值= int checkedIndex = R_Group.indexOfChild(radioButton);

但是当我按下上一个按钮时,我获得共享Prefs值为零或-1并且单选按钮未选中。

这是我的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

之后我从DDMS检查了xml文件,它没有存储值n。它还存储-1值。这里log cat显示共享的prefs值。

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

1 个答案:

答案 0 :(得分:0)

在RadioGroup中,您必须在尝试保存时创建localInstance以获取RadioGroup中当前选定的选项。我创建了最简单的例子和 您可以按照以下方式实施:

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();
}

保存此值后,您可以按如下方式检索它

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);
}


}