mardi 3 novembre 2015

How to get Result from json response everytime and increment that value in score?

java class is as follow ? I am developing game.. i which i am getting random questions everytime on click.. but i am not get answer value shown in json response below. i want to get yes_score value if yes is pressed and no_score value if no is pressed and then increment value in score variable.

But the problem is that everytime i m getting same value of yes_score and no_Score..but i have different values of yes_Score and no_Score bassed on question.. can anyone help me to solve this

public class Question extends Activity {



    ArrayList<Integer> list_qtn_25;
    String question;
    JSONObject jo_inside = null;
    String question1;
    int no_score = 0;
    int yes_score = 0;
    List<String> questions;

    private TextView tvQuestionQuestion, tvQuestionCounter, tvQuestionText, tvQuestionYes,
            tvQuestionNo, tvCounterLast;
    private String NAME_JSON;
    private ImageView imUp, imDown;
    private int counter = 1;
    private int question_counter = 1;
    private int score = 0;



    public static int randInt(int min, int max) {
        Random rand = null;
        rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_zone_question);


        list_qtn_25 = new ArrayList<>();


        tvQuestionQuestion = (TextView) findViewById(R.id.tvQuestionQuestion);
        tvQuestionCounter = (TextView) findViewById(R.id.tvQuestionCounter);
        tvQuestionText = (TextView) findViewById(R.id.tvQuestionText);
        tvQuestionYes = (TextView) findViewById(R.id.tvQuestionYes);
        tvQuestionNo = (TextView) findViewById(R.id.tvQuestionNo);
        tvCounterLast = (TextView) findViewById(R.id.tvCounterLast);

        imUp = (ImageView) findViewById(R.id.imUp);
        imDown = (ImageView) findViewById(R.id.imDown);





        // JSON
        ListQuestion();


        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();
                    }
                }
            }
        });



        imDown.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 (no_score == -1)
                        score = -1;
                    else {
                        score += no_score;
                        Log.d("no_per", String.valueOf(score));
                    }

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


    }




    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) + "");

            questions = new ArrayList<>();

            for (int i = 0; i < jsonArray.length(); i++) {
                jo_inside = jsonArray.getJSONObject(i);

                question = jo_inside.getString("question");
                Log.e("Details-->", jo_inside.getString("question"));

                /*   no_score = jo_inside.getInt("no_score");
                yes_score = jo_inside.getInt("yes_score");*/


                for (int j = 1; j <= 25; j++) {
                    if (i == Arrays.asList(arr).get(j)) {
                        tvQuestionText.setText(question);

                        Log.e("Details-->", jo_inside.getString("question"));
                        question1 = jo_inside.getString("question");
                        Log.e("Details-->", question1);

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

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

                        questions.add(question1);




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




    private void calculation() {
        Log.d("counter value", String.valueOf(score));
        Intent intent = new Intent(Question.this, Result.class);
        intent.putExtra("percentage", score);
        startActivity(intent);
    }




}

Json response :-

 E/Details-->﹕ Does he act like he is better than you?
    E/no_score-->﹕ 1
    E/yes_Score-->﹕ 3
    E/Details-->﹕ Has he ever told you he misses you?
    E/Details-->﹕ Does he react positively if you say he looks nice?
    E/no_score-->﹕ 3
    E/yes_Score-->﹕ 1
    D/yes_per﹕ 3
    yes_per﹕ 6
    D/yes_per﹕ 9

Everytime last value of yes_score is getting increment by 3 only..but i have different value of yes_Score and no_Score based on questions..how to do that




Aucun commentaire:

Enregistrer un commentaire