0
try 
{              
          //got input from asset that's not a problem

           JSONObject obj = new JSONObject(loadJSONFromAsset());

            JSONArray m_jArry1 = obj.getJSONArray("check");
            int j=0,i,k=0;

            while(j<m_jArry1.length()) {

                JSONObject jsonObject = m_jArry1.getJSONObject(j);
                JSONArray m_jArry = jsonObject.getJSONArray("formules");

                ArrayList<Integer> list = new ArrayList<Integer>(m_jArry.length());
                for (i = 0; i < m_jArry.length(); i++) {
                    list.add(i);
                }

                Collections.shuffle(list);

              //shuffled the list for random generation of questions and answer 
              // so that's not a problem too.    


                for (i = 0; i < 6; i++) {

                    JSONObject jo_inside = m_jArry.getJSONObject(list.get(i));
                    formula_value = jo_inside.getString("ques");
                    url_value = jo_inside.getString("ans");



          /*I want to read question and answer from formula_value &
            url_value each time and wait for 6 seconds until the next set arrives... 
            So I used Handler to make it wait for 6 seconds which isn't working ..         
            but  it's going to the last value  directly .*/

                    handler = new Handler();
                    handler.postDelayed(new Runnable(){
                        @Override
                        public void run() {


                            textView.setText(formula_value);


                            editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {


                //using this so that there is an action only when done is pressed .
                                @Override
                                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                                    if(actionId== EditorInfo.IME_ACTION_DONE){
                                        //Clear focus here from edittext
                                        editText.clearFocus();
                                    }
                                    if(editText.getText().equals(url_value)){
                                        total++;
                                        textView1.setText("Correct");
                             //display correct if right 
                                    }
                                    else{
                                        textView1.setText("Wrong");
                             //display wrong if wrong
                                    }

                                    return false;
                                }
                            });

                        }


                },6000);

here i have given 6 seconds .. what really happens is that the plain textView is displayed for 6 seconds and then the last question gets appeared on the screen ..

                    Log.d("For and url is ",formula_value+" and   "+url_value);
                }


                j++;
            }

        }catch (JSONException e) {
            e.printStackTrace();
        }

see the log: they get logged quickly without waiting .. Do help me pls enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

0

This is due to you are not updating your value in handler's runnable. Handler.Postdelay not pausing the loop. The loop is being executed. and your value is being updated prior to 6 seconds. And when your 6 seconds ends you will get last value always as your loop is executed.

One more thing I observed is that You are using nested loop here. I suggest you to use recursion in this condition. Thanks

Mushahid Khatri
  • 728
  • 4
  • 12
0

I found the problem thank you .. I was using a for loop which is a big mistake. I now have a big doubt on how to stop the handler. I used

    mHandler.removeCallbacksAndMessages(null);

                or

    mHandler.removeCallbacks(runnable);

and did all possible steps to stop the process. nothing is working out, there is an infinite loop execution. I don't know how to stop it :(

C here bro : handler.removecallbacks not working

Community
  • 1
  • 1