How to save RadioButton status with SharedPreference?

两盒软妹~` 提交于 2019-12-25 09:26:05

问题


This is the app view:

and here is my activity code:

public class Quiz extends AppCompatActivity {
    public static int point;
    public static int i = 0;
    RadioGroup radioGroup;
    public static int totalPoint =0;
    int arrValues[]={0,0,0,0};
    int iPosition=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        i = 0;
       final RadioButton radioButton1 =(RadioButton) findViewById(R.id.number_1);
        final RadioButton radioButton2 =(RadioButton) findViewById(R.id.number_2);
        final RadioButton radioButton3 =(RadioButton) findViewById(R.id.number_3);
        final RadioButton radioButton4 =(RadioButton) findViewById(R.id.number_4);
        final String[] items = getResources().getStringArray(R.array.quiz);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        Button nextButton = (Button) findViewById(R.id.button);
        Button previousButton = (Button) findViewById(R.id.button2);
        radioButton1.setText(items[0]);
        radioButton2.setText(items[1]);
        radioButton3.setText(items[2]);
        radioButton4.setText(items[3]);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) throws ArrayIndexOutOfBoundsException {
               try {
                   if (checkedId == R.id.number_1) {
                       arrValues[iPosition] = 0;
                   } else if (checkedId == R.id.number_2) {
                       arrValues[iPosition] =  1;
                   }else if (checkedId == R.id.number_3) {
                       arrValues[iPosition]=  2;
                   }else if (checkedId == R.id.number_4) {
                       arrValues[iPosition] =  3;
                   }
                   // Total
                   totalPoint = getTotalPoint();

                   Log.e("sd",""+totalPoint);
               }
                catch (ArrayIndexOutOfBoundsException sd){
                    Log.e("sd","sdsd");
                }
            }

            private int getTotalPoint() {

                int sum = 0;
                for (int i = 0; i < arrValues.length; i++)
                    sum += arrValues[i];
                Log.e("sum",""+sum);

                return sum;
            }
        });
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                loadNextQuestion();
            }

            private void loadNextQuestion() throws ArrayIndexOutOfBoundsException {
                // Update UI
                // Clear radio button state
                try {
                    if (i>= items.length-4){
                        return;
                    }
                    iPosition++;
                    i = i + 4;
                    radioGroup.clearCheck();
                    radioButton1.setText(items[i]);
                    radioButton2.setText(items[i + 1]);
                    radioButton3.setText(items[i + 2]);
                    radioButton4.setText(items[i + 3]);
                } catch (ArrayIndexOutOfBoundsException arr) {

                }

            }
        });
        previousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Update UI
                // Clear radio button state
                loadpreQuestion();


            }

            private void loadpreQuestion() {
                try {
                    if (i<=0){
                       return;
                    }
                    iPosition--;
                    i = i - 4;
                    radioGroup.clearCheck();
                    radioButton1.setText(items[i]);
                    radioButton2.setText(items[i + 1]);
                    radioButton3.setText(items[i + 2]);
                    radioButton4.setText(items[i + 3]);
                } catch (ArrayIndexOutOfBoundsException arr) {

                }
            }
        });
    }
    public void onRadioButtonClicked(View view)throws ArrayIndexOutOfBoundsException {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        try {

        switch(view.getId()) {
            case R.id.number_1:
                if (checked)
                    point = 0;
                Toast.makeText(this, ""+ arrValues[iPosition] ,
                        Toast.LENGTH_SHORT).show();
                // Pirates are the best
                break;
            case R.id.number_2:
                if (checked)
                    point = 1;
                Toast.makeText(this, ""+ arrValues[iPosition] ,
                        Toast.LENGTH_SHORT).show();
                // Ninjas rule
                break;
            case R.id.number_3:
                if (checked)
                    point = 2;
                Toast.makeText(this, ""+arrValues[iPosition] ,
                        Toast.LENGTH_SHORT).show();
                // Ninjas rule
                break;
            case R.id.number_4:
                if (checked)
                    point = 3;
                Toast.makeText(this, ""+ arrValues[iPosition] ,
                        Toast.LENGTH_SHORT).show();
                // Ninjas rule
                break;   }

        }
        catch (ArrayIndexOutOfBoundsException ds){}
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        i = 0;
    }


}

As you see there are only 4 RadioButtons at a time and their text just change with Next and Previous buttons. So how can I save RadioButton status with SharedPrefrence without affecting other questions?

UPDATE CODE:

     @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) throws ArrayIndexOutOfBoundsException {
               try {
                   if (checkedId == R.id.number_1) {
                       arrValues[iPosition] = 0;
                       editor.putInt(String.valueOf(iPosition), 0);

                   } else if (checkedId == R.id.number_2) {
                       arrValues[iPosition] =  1;
                       editor.putInt(String.valueOf(iPosition), 1);
                       sharedPreferences.getInt(String.valueOf(iPosition),1);
                       int radioButtonStatusQ1 = sharedPreferences.getInt("0", 0);
                       if (radioButtonStatusQ1==1){
                           radioGroup.check(R.id.number_2);

                       }

                   }else if (checkedId == R.id.number_3) {
                       arrValues[iPosition]=  2;
                       editor.putInt(String.valueOf(iPosition), 2);
                   }else if (checkedId == R.id.number_4) {
                       arrValues[iPosition] =  3;
                       editor.putInt(String.valueOf(iPosition), 3);
                   }


                   // Commit
                   editor.commit();
                   // Total
                   totalPoint = getTotalPoint();

                   Log.e("sd",""+totalPoint);
               }
                catch (ArrayIndexOutOfBoundsException sd){
                    Log.e("sd","sdsd");
                }
            }

回答1:


Store your RadioButton status into SharedPreferences with KEY iPosition and iPosition is 0, 1, 2, 3 ......(N-1) (where N = Number of question).

Store RadioButton status in SharedPreferences:

public class Quiz extends AppCompatActivity {

    public static int point;
    public static int i = 0;
    RadioGroup radioGroup;
    public static int totalPoint = 0;
    int arrValues[] = {0,0,0,0};
    int iPosition = 0;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


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

        .......
        .............

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        editor = sharedPreferences.edit();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) throws ArrayIndexOutOfBoundsException {
               try {
                   if (checkedId == R.id.number_1) {
                       arrValues[iPosition] = 0;
                       editor.putInt(String.valueOf(iPosition), 0);
                   } else if (checkedId == R.id.number_2) {
                       arrValues[iPosition] =  1;
                       editor.putInt(String.valueOf(iPosition), 1);
                   }else if (checkedId == R.id.number_3) {
                       arrValues[iPosition]=  2;
                       editor.putInt(String.valueOf(iPosition), 2);
                   }else if (checkedId == R.id.number_4) {
                       arrValues[iPosition] =  3;
                       editor.putInt(String.valueOf(iPosition), 3);
                   }

                   // Commit changes
                   editor.commit();

                   // Total
                   totalPoint = getTotalPoint();

                   Log.e("sd",""+totalPoint);
               }
                catch (ArrayIndexOutOfBoundsException sd){
                    Log.e("sd","sdsd");
               }
            }
        });

    }

    ............
    ...............

}

Get RadioButton status from SharedPreferences:

int radioButtonStatusQ1 = sharedPreferences.getInt("0", 0);
int radioButtonStatusQ2 = sharedPreferences.getInt("1", 0);
........
..................


来源:https://stackoverflow.com/questions/44331848/how-to-save-radiobutton-status-with-sharedpreference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!