public class MainActivity extends AppCompatActivity {
int total = 0,k=0,j=0,i;
public EditText editText;
private int mInterval = 1000;
Handler mHandler = new Handler();
String formula_value,url_value;
TextView textView,textView1;
ArrayList<Integer> list;
JSONArray m_jArry,m_jArry1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
textView = (TextView)findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
textView1 = (TextView) findViewById(R.id.val);
//for focusing the edittext when clicked.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//Clear focus here from edittext
editText.clearFocus();
}
return false;
}
});
try {
//for focusing my JSON file no problem here.
JSONObject obj = new JSONObject(loadJSONFromAsset());
m_jArry1 = obj.getJSONArray("check");
//calling the handler
startRepeatingTask();
}catch (JSONException e) {
e.printStackTrace();
}
}
OnDestroy() to destroy the handler, this is where I don't know what to do
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
stopRepeatingTask();
}
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
This is heart of my app
public void updateStatus(){
try {
JSONObject jo_inside = m_jArry.getJSONObject(list.get(k));
k++;
formula_value = jo_inside.getString("ques");
url_value = jo_inside.getString("ans");
textView.setText(formula_value);
I get the ques and answer from the JSON file and check if the value given in edittext is same as answer value from JSON file. But there is another problem I should look on after figuring this stopping of handler problem which is By the time I type the answer for question 1 in edittext, the 1st loop gets over and the compiler checks the answer I typed with the answer of question 2. I should find a way to slow down the process for execution
if (editText.getText().toString().equals(url_value)) {
total++;
textView1.setText("Correct");
} else {
textView1.setText("Wrong");
}
editText.setText(" ");
if(k==6){
k=0;
j++;
}
}
catch (JSONException e){
e.printStackTrace();
}
}
My Runnable :
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
try {
if(k==0){
try {
//The handler should stop when j is equal to or greater than Array size which is 5 in this case but it keeps on looping.
if(j>=m_jArry1.length()){
stopRepeatingTask();
}
JSONObject jsonObject = m_jArry1.getJSONObject(j);
m_jArry = jsonObject.getJSONArray("formules");
list = new ArrayList<Integer>(m_jArry.length());
for (i = 0; i < m_jArry.length(); i++) {
list.add(i);
}
Collections.shuffle(list);
}catch(JSONException e){
e.printStackTrace();
}
}
updateStatus();
} finally {
mHandler.postDelayed(mStatusChecker, mInterval);
}
}
};
//I removed this loadJSONFromAsset() function because i have no problem in it.
}
Sanjay change your OnDestroy() function to