2017-03-16 90 views
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); 

在这里,我已经给6秒..真正的情况是,普通的TextView显示6秒钟,然后最后一个问题被出现在屏幕上..处理器不工作按计划

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


       j++; 
      } 

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

查看日志:他们得到迅速登录,而无需等待.. 请帮我请 enter image description here

回答

0

这是因为你没有更新的处理程序的可运行你的价值。 Handler.Postdelay不会暂停循环。循环正在执行。并且您的价值在6秒之前正在更新。当你的6秒钟结束时,你的循环被执行时你会得到最后一个值。

我观察到的另一件事是你在这里使用嵌套循环。我建议你在这种情况下使用递归。谢谢

0

我发现这个问题谢谢..我正在使用for循环,这是一个很大的错误。我现在对如何停止处理程序有很大疑问。我用

mHandler.removeCallbacksAndMessages(null); 

       or 

    mHandler.removeCallbacks(runnable); 

并采取了所有可能的措施来停止该过程。没有什么工作,有一个无限循环执行。我不知道如何阻止它:(

C here bro:handler.removecallbacks not working