Getting constant false value from method

前端 未结 3 505
迷失自我
迷失自我 2021-01-21 17:42

I have 3 questions

 private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolea         


        
相关标签:
3条回答
  • 2021-01-21 18:01

    Where are you updating isAnswered array. In the code snippet you have provided, it updates only answer and checked arrays.

    Probably that is why you are always getting false for all 3 array elements of isAnswered array.

    Where are you updating isAnswered array. In the code snippet you have provided, it updates only answer and checked arrays.

    Probably that is why you are always getting false for all 3 array elements of isAnswered array.

    Hi Roman,

    I believe you have multiple radio groups each with 3 radio buttons. If that is the case, just slight modification in you above code snippet should work for you.

    rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.radioButton1) {
                Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
                checked[0] = true;
                answer[0] = true;
            } else {
                checked[0] = true;
                answer[0] = false;
            }
            // update isAnswered[0] to true. as this gets invoked when you choose any radio button.
            isAnswered[0] = true;
        }
    });
    

    Please let me know if this helps.

    0 讨论(0)
  • 2021-01-21 18:15

    I have implemented a quick class to validate your logic and found no problems in your implementation.

    public class MainActivity extends AppCompatActivity {
    
        private static int NUMBER_OF_QUESTIONS = 3;
        static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setBooleanValues();
            allAnswersChecked();
        }
    
        private void setBooleanValues() {
            isAnswered[0]=true;
            isAnswered[1]=false;
            isAnswered[2]=true;
        }
    
        private void allAnswersChecked() {
            for (boolean radioAnswer : isAnswered) {
                if (!radioAnswer) {
                    //return false;
                    Log.d("allAnswersChecked","false");
                }
                else {
                    Log.d("allAnswersChecked","true");
                }
            }
            //return true;
        }
    
    }
    

    See the log output :

    05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: true
    05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: false
    05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: true
    

    So I recomment to print values and check if it is being correct set into your isAnswered array before calling allAnswersChecked().

    0 讨论(0)
  • 2021-01-21 18:28

    Try this:

    static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS]; // initially all false
    
    .............
    ....................
    
    rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.radioButton1) {
                Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
                checked[0] = true;
                answer[0] = true;
            } else {
                checked[0] = true;
                answer[0] = false;
            }
    
            // Update value as user selected radio button
            isAnswered[0] = true;
        }
    });
    

    Do same for your other RadioGroups(rg2, rg3, ......). Hope this will help~

    0 讨论(0)
提交回复
热议问题