2015-10-21 29 views
-1

//規範JSON響應和解析如何從按鈕點擊事件的json響應獲得隨機問題?

private void ListQuestion() { 
     try { 
      if (FriendZoneSelectFriendsWith.choose_gender.equals("Boy")) { 
       NAME_JSON = "boysquestion.json"; 
      } else { 
       NAME_JSON = "girlsquestion.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()); 
      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) + ""); 

      for (int i = 1; i < jsonArray.length(); 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)) { 
         Log.e("Details-->", jo_inside.getString("question")); 
         question1 = jo_inside.getString("question"); 
         tvQuestionText.setText(question1); 

         //Change question on click 
         imUp.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           for (int k = 1; k <= 25; k++) { 
            if (k == Arrays.asList(arr).get(k)) { 
             Log.d("textview", tvQuestionText.getText().toString()); 
             tvQuestionText.setText(question1); 
            } 

           } 
          } 
         }); 
         // Log*/.d("Textview", tvQuestionText.getText().toString()); 
        } 


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

// Json的應答爲

Details-->﹕ Does he message you asking for advice? 
10-21 02:52:40.157 7878-7878/? E/Details-->﹕ Has he ever sent you multiple messages in a row? 
10-21 02:52:40.157 7878-7878/? E/Details-->﹕ Has he rang you crying over a girl? 
10-21 02:52:40.157 7878-7878/? E/Details-->﹕ When you see him, do his text habits for others match what he is doing with you? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Are you waiting days for a response to a text message? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Does he send you message telling you he is thinking about you or similar? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Does he comment on your Facebook but not write back to your text messages? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Does he use social media to contact you rather than phone? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Has he ever has a photo with you as his profile picture? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Has his friends started randomly adding you on Facebook or followed you on Instagram/Twitter? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Does he ask you to hold his stuff regularly? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Has he burped in front of you? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Has he farted in front of you? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Does he listen when you speak? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Do you leave your friends to go and hang out with him and his friends? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Have you been on multiple dates with her? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Did he get dressed up for the date? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Did you talk about deep things on the date? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Did you insist on paying, only for him not to let you? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Did you he drop you home? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ When you dropped her home, did he linger in the car for a long time? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Did he take a photo and post it online while you were on your date? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Do his friends talk to you? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Have you ever sent him a thoughtful gift/message and he has not said thank you/responded? 
10-21 02:52:40.168 7878-7878/? E/Details-->﹕ Has he ever asked you to cook for him? 

我想隨機抽題,然後將其設置按鈕,點擊後的TextView。 我如何獲得按鈕單擊的隨機問題。幫助我的示例代碼。我嘗試了一些東西,但都沒有成功。

回答

1

你可以把你所有的問題放在一個列表中,然後他們訪問這個列表中的一個隨機元素。

從這個thread使用randInt(int min, int max)獲得範圍之間的隨機數,你可以做(​​未測試)

List<String> questions = new ArrayList<>(); 
//Add elements to the list of questions 
//questions.add(...); 
//questions.add(...); 

if(!questions.isEmpty()) 
    String randomQuestion = questions.get(randInt(0,questions.size()); 

編輯 每次你點擊按鈕時,你需要得到aother隨機來自你的清單的問題。

因此,使用你的代碼已經像做(未測試)

//Initialize list of questions 
List<String> questions = new ArrayList<>(); 
for (int i = 1; i < jsonArray.length(); i++) { 
    jo_inside = jsonArray.getJSONObject(i); 
    question = jo_inside.getString("question"); 
    questions.add(question); 
} 

//On click get another random question from list 
imUp.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if(!questions.isEmpty()) { 
      String randomQuestion = questions.get(randInt(0,questions.size()); 
      tvQuestionText.setText(randomQuestion); 
     } 
    } 
}); 

,並從該thread

/** 
* Returns a pseudo-random number between min and max, inclusive. 
* The difference between min and max can be at most 
* <code>Integer.MAX_VALUE - 1</code>. 
* 
* @param min Minimum value 
* @param max Maximum value. Must be greater than min. 
* @return Integer between min and max, inclusive. 
* @see java.util.Random#nextInt(int) 
*/ 
public static int randInt(int min, int max) { 

    // NOTE: This will (intentionally) not run as written so that folks 
    // copy-pasting have to think about how to initialize their 
    // Random instance. Initialization of the Random instance is outside 
    // the main scope of the question, but some decent options are to have 
    // a field that is initialized once and then re-used as needed or to 
    // use ThreadLocalRandom (if using at least Java 1.7). 
    Random rand; 

    // nextInt is normally exclusive of the top value, 
    // so add 1 to make it inclusive 
    int randomNum = rand.nextInt((max - min) + 1) + min; 

    return randomNum; 
} 
+0

我已經有隨機抽題隨機方法......如何改變問題按鈕單擊並將它們設置爲textview –

+0

您需要點擊另一個隨機問題(請參閱編輯) – ThomasThiebaud

+0

可以向我顯示randInt方法用於我的用法 –