doInBackground() で json 関数を使用する asynctask があり、変数を onPostExecute に返します。問題は、json 関数を while ループにしたいということです。ループに入るたびに、コメントが収集されます。したがって、15 個のコメントを持つことができ、それらすべてを onPostExecute に送信してビューに配置したいと考えています。これが私のコードです
class loadComments extends AsyncTask<JSONObject, String, List<String>> {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
protected List<String> doInBackground(JSONObject... params) {
//do your work here
List<String> results = new ArrayList<String>();
int i = 0;
while (i < 5 ){
JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);
result = json2;
results.add(result);
i++;
}
return results;
}
@Override
protected void onPostExecute(List<String> results) {
try {
if (json2.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res2 = json2.getString(KEY_SUCCESS);
if(Integer.parseInt(res2) == 1){
commentView.setText(json2.getString(KEY_COMMENT));
LinearLayout.LayoutParams commentViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
commentViewParams.setMargins(20, 10, 20, 20);
commentView.setBackgroundResource(R.drawable.comment_bg);
commentView.setTextColor(getResources().getColor(R.color.black));
commentBox.addView(commentView, commentViewParams);
}//end if key is == 1
else{
// Error in registration
registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
}//end else
}//end if
} //end try
catch (JSONException e) {
e.printStackTrace();
}//end catch
}
}
このコードは機能しませんが、私が達成しようとしていることの例です