mardi 3 novembre 2015

Getting last value from json everytime from loop

Everytime when i click button "imUp" score is not getting increment by value from json.e.g. if last json value is 1..everytime score is incremented by 1 only.. i have different values..my json response is below..

 private void ListQuestion() {
            try {
                if (FriendsWith.choose_gender.equals("Boy")) {
                    NAME_JSON = "question1.json";
                } else {
                    NAME_JSON = "question2.json";
                }
                BufferedReader jsonReader = new BufferedReader(new InputStreamReader(getAssets().open(NAME_JSON)));
                StringBuilder jsonBuilder = new StringBuilder();
                for (String line = null; (line = jsonReader.readLine()) != null; ) {
                    jsonBuilder.append(line).append("\n");
                }

                // Parse Json
                JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
                final JSONArray jsonArray = new JSONArray(tokener);

                Log.e("JSON LENGTH-->", jsonArray.length() + "");

                final Integer[] arr = new Integer[jsonArray.length()];
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = i;
                }
                Collections.shuffle(Arrays.asList(arr));
                Log.e("RANDOM", Arrays.asList(arr) + "");

                final List<String> questions = new ArrayList<>();

                for (int i = 1; i < jsonArray.length() && i<=25; i++) {
                    jo_inside = jsonArray.getJSONObject(i);

                    question = jo_inside.getString("question");

                    for (int j = 1; j <= 25; j++) {

                            if (i == Arrays.asList(arr).get(j)) {
                                jo_inside = jsonArray.getJSONObject(j);
                                Log.e("Details-->", jo_inside.getString("question"));


                                question1 = jo_inside.getString("question");
                                Log.e("question 25-->", question1);
                                tvQuestionText.setText(question1);



                            yes_score = jo_inside.getInt("yes_score");
                            Log.e("yes_Score-->", jo_inside.getString("yes_score"));


                                questions.add(question1);


                                //On click get another random question from list

                                imUp.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                        question_counter++;
                                        tvQuestionCounter.setText(String.valueOf(question_counter));
                                        if (!questions.isEmpty()) {

                                            String randomQuestion = questions.get(randInt(0, questions.size() - 1));
                                            tvQuestionText.setText(randomQuestion);


                                            if (yes_score == -1)
                                                score = -1;
                                            else {
                                                score += yes_score;
                                                Log.d("yes_per", String.valueOf(score));
                                            }

                                            counter++;
                                            if (counter == 25) {
                                                calculation();
                                            }
                                        }
                                    }


                                });




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

        }

json resposne :-

E/Details-->﹕ Does he give her number out to a lot of people?
11-03 05:25:36.873  24520-24520/com.example.. E/question 25-->﹕ Does he give her number out to a lot of people?
11-03 05:25:36.873  24520-24520/com.example.. E/no_score-->﹕ 1
11-03 05:25:36.873  24520-24520/com.example.. E/yes_Score-->﹕ 3
11-03 05:25:36.873  24520-24520/com.example.. E/Details-->﹕ Does he have you saved under a nickname in his phone?
11-03 05:25:36.873  24520-24520/com.example.. E/question 25-->﹕ Does he have you saved under a nickname in his phone?
11-03 05:25:36.873  24520-24520/com.example.. E/no_score-->﹕ 0
11-03 05:25:36.873  24520-24520/com.example.. E/yes_Score-->﹕ 4
11-03 05:25:36.873  24520-24520/com.example.. E/Details-->﹕ Does he message other boys while hanging out with you?
11-03 05:25:36.873  24520-24520/com.example.. E/question 25-->﹕ Does he message other boys while hanging out with you?
11-03 05:25:36.873  24520-24520/com.example.. E/no_score-->﹕ 1
11-03 05:25:36.873  24520-24520/com.example.. E/yes_Score-->﹕ 3
11-03 05:25:36.873  24520-24520/com.example.. E/Details-->﹕ Does he laugh a lot on the phone?
11-03 05:25:36.873  24520-24520/com.example.. E/question 25-->﹕ Does he laugh a lot on the phone?
11-03 05:25:36.873  24520-24520/com.example.. E/no_score-->﹕ 3
11-03 05:25:36.873  24520-24520/com.example.. E/yes_Score-->﹕ 1

--my question is why i am not getting different value i.e value according to question ..why last json value is getting repeated.. everytime i press imUp button score value is incremented by 1 only or last yes_score value.. but yes_Score value is different according to score.. can anyone help me..plz

my output :-

11-03 05:25:37.573  24520-24520/com.example.. D/yes_per﹕ 1
11-03 05:25:37.753  24520-24520/com.example.. D/yes_per﹕ 2
11-03 05:25:37.943  24520-24520/com.example.. D/yes_per﹕ 3
11-03 05:25:38.313  24520-24520/com.example.. D/yes_per﹕ 4




Aucun commentaire:

Enregistrer un commentaire