2017-08-16 4 views
-4

をListViewコントロールするArrayListにJSON配列を取得:私は、質問オブジェクトのこの配列を返すPHPページ(urldisplay)を持っている

[{ 
"suggestid": "1", 
"question": "What does Aotearoa mean?", 
"answer1": "Maui and the Whale", 
"answer2": "2 Islands", 
"answer3": "Land of the Long White Cloud", 
"answer4": "Isle of Greenstone", 
"correctanswer": "Land of the Long White Cloud", 
"userID": "1" 
}, { 
"suggestid": "2", 
"question": "What does Haere Ra mean?", 
"answer1": "Hello", 
"answer2": "Goodbye", 
"answer3": "Thanks", 
"answer4": "Sorry", 
"correctanswer": "Goodbye", 
"userID": "1" 
}, { 
"suggestid": "3", 
"question": "Where is Zealand?", 
"answer1": "France", 
"answer2": "Germany", 
"answer3": "Denamrk", 
"answer4": "Finland", 
"correctanswer": "Denmark", 
"userID": "1" 
}, { 
"suggestid": "4", 
"question": "What does Aotearoa mean?", 
"answer1": "Maui and the Whale", 
"answer2": "2 Islands", 
"answer3": "Land of the Long White Cloud", 
"answer4": "Isle of Greenstone", 
"correctanswer": "Land of the Long White Cloud", 
"userID": "2" 
    }, { 
"suggestid": "5", 
"question": "What does Aotearoa mean?", 
"answer1": "Maui and the Whale", 
"answer2": "2 Islands", 
"answer3": "Land of the Long White Cloud", 
"answer4": "Isle of Greenstone", 
"correctanswer": "Land of the Long White Cloud", 
"userID": "3" 
}] 

私は(私のDAOクラスで)このクラス経由でオブジェクトを読み取るために保存するしようとしています結果は配列のリストに配列されます

ArrayList<Question> quest; 

    public ArrayList<Question> ListSugg() 
{ 
    ListSuggestions sugg = (ListSuggestions)new ListSuggestions().execute(); 
    return quest; 
} 

public class ListSuggestions extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 

     try { 
      URL url = new URL(urldisplay); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      JSONArray jsonArray = new JSONArray(con); 
      quest = new ArrayList<Question>(); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
       String question = jsonObject.getString("question"); 
       String answer1 = jsonObject.getString("answer1"); 
       String answer2 = jsonObject.getString("answer2"); 
       String answer3 = jsonObject.getString("answer3"); 
       String answer4 = jsonObject.getString("answer4"); 
       String correctanswer = jsonObject.getString("correctanswer"); 
       Question ques = new Question(0, question, answer1, answer2, answer3, answer4, correctanswer); 
       quest.add(ques); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e1) { 
      e1.printStackTrace(); 
     } 
     return null; 
    } 
} 

クエストは空ですが。次に、質問オブジェクトをListViewに追加します。

私は他のSOの投稿を試しましたが、私は自分の尻尾を追いかけているようです。誰でも私の間違いを見つけることができますか/これを行うにはより良い方法がありますか?

乾杯、

+0

あなたは直面しているエラーは何ですか? –

+0

デバッグヘルプ(「なぜこのコードは動作していませんか?」)を検索するには、目的の動作、特定の問題またはエラー、および質問自体に再現するのに必要な最短コードが含まれている必要があります。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[最小限で完全で検証可能な例を作成する方法](https://stackoverflow.com/help/mcve) – LazerBanana

+0

jsonarrayは空ですので、forループで失敗します – Gremio

答えて

1

問題は、AsyncTaskがコードAsunchrounslyを実行するということです。つまり、ListSugg()を呼び出すと、別のスレッドでバックグラウンドタスクが実行され、バックグラウンドクエストが完了する前にクエストオブジェクトが返されます。リスト。あなたは何ができるかread here for more info

は、あなたがonPostExecuteメソッドをオーバーライドし、そのように、そこにアダプターを設定することです:

public class ListSuggestions extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Boolean doInBackground(String... params) { 
     // do something 
     return true; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // The results of the above method 
     // Processing the results here 

     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      list); 

     listv.setAdapter(arrayAdapter); 

    } 

} 

をしかし、これが機能するために、あなたはMainActivity中にあなたAsyncTaskを含める必要があります。

これはあなたの問題を解決するはずです。

しかし、okhttpやRetrofitのようなHTTPネットワーキングライブラリを使用し、GsonのようなJSONパーサーを使用するなど、もっと簡単で簡単なソリューションがあります。

+0

これは正しい方向に私を指摘します! – Gremio

0

あなたはできるだけ早くあなたがバックグラウンドタスクを発射としてquesを戻ってきています。バックグラウンドタスクは完了するまでに時間がかかり、quesリストはnullのままなのでデータが表示されません。

postExecuteのAsyncTaskメソッドのリストを更新します。

0

あなたはdoInBackground()からnullを返すされているので、それが空であるこの

 quest = new ArrayList<Question>(); 
     for (int i = 0; i < jsonArray.length(); i++) { 

      JSONObject jsonObject = jsonArray.getJSONObject(i); 

      Question ques = new Question(); //inti Question 
      //Enter Values to Question Object 
      ques.setQuestion(jsonObject.getString("question")); 
      ques.setAnswer1(jsonObject.getString("answer1")); 
      ques.setAnswer2(jsonObject.getString("answer2")); 
      ques.setAnswer3(jsonObject.getString("answer3")); 
      ques.setAnswer4(jsonObject.getString("answer4")); 
      ques.setCorrectAnswer(jsonObject.getString("correctanswer")); 
      // Add Question Object to ArrayList 
      quest.add(ques); 
     } 
0

を試してみてください。理想的な方法は、結果をonPostExecute()に渡して、アダプタをそこに設定することです。 -

public class ListSuggestions extends AsyncTask<Void, Void, ArrayList<Question>> { 

    @Override 
    protected ArrayList<Question> doInBackground(Void... params) { 

     try { 
      URL url = new URL(urldisplay); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      //JSONArray jsonArray = new JSONArray(con); 
      quest = new ArrayList<Question>(); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
       String question = jsonObject.getString("question"); 
       String answer1 = jsonObject.getString("answer1"); 
       String answer2 = jsonObject.getString("answer2"); 
       String answer3 = jsonObject.getString("answer3"); 
       String answer4 = jsonObject.getString("answer4"); 
       String correctanswer = jsonObject.getString("correctanswer"); 
       Question ques = new Question(0, question, answer1, answer2, answer3, answer4, correctanswer); 
       quest.add(ques); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e1) { 
      e1.printStackTrace(); 
     } 
     return quest; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<Question> questions) { 
     super.onPostExecute(questions); 

     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
       this, 
       android.R.layout.simple_list_item_1, 
       list); 

     listv.setAdapter(arrayAdapter); 
    } 
} 
関連する問題