2016-03-25 31 views
0

Merhaba, garip bir sorunla karşı karşıyayım. Ben mysql veritabanından soru ve cevapları ile bir sınav uygulaması yapmaya çalışıyorum. here göründüğü gibi değerleri ayrıştırıyorum.Arraylist doğru şekilde ayrıştırılmadı

i oluşturmak nasıl List:

@Override 
    protected List<QuestionsList> doInBackground(String... params) { 
     nameValuePairs = new ArrayList<>(); 
     try { 
      url = new URL(params[0]); 
      httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setReadTimeout(10000); 
      httpURLConnection.setConnectTimeout(15000); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoInput(true); 
      httpURLConnection.setDoOutput(true); 
      setupDataToDB(); 
      outputStream = httpURLConnection.getOutputStream(); 
      bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); 
      bufferedWriter.write(StringGenerator.queryResults(nameValuePairs)); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      outputStream.close(); 
      httpURLConnection.connect(); 
      inputStream = new BufferedInputStream(httpURLConnection.getInputStream()); 
      jsonResult = StringGenerator.inputStreamToString(inputStream, QuestionsActivity.this); 
      jsonResponse = new JSONObject(jsonResult.toString()); 
      Log.e("Response: ", jsonResult.toString()); 
      checkDisplayLanguage(langText); 
      questionsLists = new ArrayList<>(); 
      for (int i = 0; i < jsonMainNode.length(); i++) { 
       jsonChildNode = jsonMainNode.getJSONObject(i); 
       questionName = jsonChildNode.optString(Constants.QUESTION_NAME_JSON_NAME); 
       Log.e("Question Name: ", questionName); 
       jsonArray = new JSONArray(jsonChildNode.optString(Constants.QUESTIONS_ANSWERS_ARRAY)); 
       for (int j = 0; j < jsonArray.length(); j++) { 
        jsonSecondChildNode = jsonArray.getJSONObject(j); 
        answer1 = jsonSecondChildNode.optString("answer1"); 
        answer2 = jsonSecondChildNode.optString("answer2"); 
        answer3 = jsonSecondChildNode.optString("answer3"); 
        iscorrect1 = jsonSecondChildNode.optString("iscorrect1"); 
        iscorrect2 = jsonSecondChildNode.optString("iscorrect2"); 
        iscorrect3 = jsonSecondChildNode.optString("iscorrect3"); 
        question_answers = new ArrayList<>(); 
        question_answers.add(answer1); 
        question_answers.add(answer2); 
        question_answers.add(answer3); 
        question_iscorrect = new ArrayList<>(); 
        question_iscorrect.add(iscorrect1); 
        question_iscorrect.add(iscorrect2); 
        question_iscorrect.add(iscorrect3); 
        Log.e("Answers in for loop", question_answers.toString()); 
        questionsLists.add(new QuestionsList(questionName, answersArray, question_iscorrect)); 
       } 

      } 
     } catch (IOException | JSONException e) { 
      e.printStackTrace(); 
     } 
     return questionsLists; 
    } 

ve sonrası gibi görünür yürütün:

@Override 
     protected void onPostExecute(List<QuestionsList> lists) { 
      super.onPostExecute(lists); 
      pDialog.dismiss(); 
      position = 0; 
      question.setText(lists.get(position).getName()); 
      answersArray = lists.get(position).getAnswers(); 
      ans1 = answersArray.get(position); 
      ranswer1.setText(ans1); 
     } 

ayıklama bilgileri şu şekilde görünür:

E/Question Name:: Where to look to find journal articles 
E/Answers in for loop: [In the librarys catalog , , ] 
E/Answers in for loop: [, In alphabetical list of healink , ] 
E/Answers in for loop: [, , Databases available in the library's site] 
E/Question Name:: What information we provide magazine 
E/Answers in for loop: [Published research experiments current information, , ] 
E/Answers in for loop: [, Lists information about people, addresses, organizations, ] 
E/Answers in for loop: [, , Legislation, competitions] 
E/Question Name:: What is the E/Answers in for loop: [Is the number used for the registration of periodical publications, , ] 
E/Answers in for loop: [, Is the International Unique number used for registration of printed books, ] 
E/Answers in for loop: [, , Is the International Unique number used for the recording of Publications mixed forms] 

Ben ne yapıyorum yanlış burada ve arilist doğru olarak ayrıştırılmamış mı?

+0

neden GSON kullanmıyorsun? –

+0

, verilerin doğru şekilde alınmasından bu yana nasıl ayrıştırıldığım ile ilgili değildir. Soru, neden hashmap'ın son maddesini aldığına dair. –

+0

Hata ayıklamadınız mı? –

cevap

3

Bu, hata ayıklamaya ve StackOverflow'a değil, yavaş yavaş gitmeye ihtiyaç duyan bir yazım hatası veya başka bir hatadır.

Kişisel bahisim, etkinliğiniz başlatılırken oluşan bir hata nedeniyle, görünümlerinizin (answer1, answer2 ve answer3'te tutulanlar) hepsi ve aynı görünüm olması. Fakat açıkçası, bu sorun belirli bir problem göstermediği için kapatılmalıdır.

Ayrıca ekleyin:

question_answers = new HashMap<String,String>(); 

satırdan önce:

question_answers.put(Constants.FIRST_ANSWER_POST_NAME, answer1); 
+0

Ne demek özel bir problem göstermiyor? Ekran görüntüsünü gördün mü?Nasıl düzeltmem gerekiyor? –

+0

Tam olarak. Ekran görüntüsünü ve kodun yaklaşık% 10'unu gösterir. Bu belirli bir sorun değil, hata ayıklama görevi. Örneğin, size mükemmel bir yanıt verdim - Belirli bir problemi tespit ettiğimde hiçbir fikrim yok, ama kodunuzun çalışmasının birçok olası sebebinden biri. – fdreger

+0

Mükemmel bir cevap nedir? aktiviteyi başlatan bir hata yüzünden mi? Bu ne anlama geliyor? Bu hangi böcek? –

1

Kodunuzda bir sorun var. Sen iç for loop içindeki

questionsLists.add(new QuestionsList(questionName, question_answers)); 

taşımak zorunda. Çünkü şu an ne yapıyorsunuz questionsLists ürününüze sadece son question_answers ekliyorsunuz. Bakılmalıdır -

for (int i = 0; i < jsonMainNode.length(); i++) { 
       .... 
       for (int j=0;j<jsonArray.length();j++){ 
        .... 

      questionsLists.add(new QuestionsList(questionName, question_answers)); 
       } 


      } 

Bu yardımcı olur umarız!

+0

evet yaptım ve aynı sonuç var –

+0

Görüşleriniz tanımlanmış ve örneklendirilmiş - answer1, answer2 ve .. için? –