我有一个AsyncTask
使用 JSON 函数来获取方法中的注释的doInBackground
方法。然后它将注释数组发送到onPostExecute
它JSONArray
放入的位置,String[]
然后将字符串放入setListAdapter
。代码没有崩溃,一切似乎都正常,但注释没有显示在Activity
.
class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
@Override
protected JSONObject doInBackground(JSONObject... params) {
final JSONObject json2 = CollectComments.collectComments(usernameforcomments,
offsetNumber);
return json2;
}
@Override
protected void onPostExecute(JSONObject json2) {
try {
if (json2.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
final String res2 = json2.getString(KEY_SUCCESS);
if (Integer.parseInt(res2) == 1) {
final JSONArray array = json2.getJSONArray(KEY_COMMENT);
final String comments[] = new String[array.length()];
for (int i = 1; i <= array.length(); i++) {
comments[i] = array.getString(i);
}
setListAdapter(new ArrayAdapter<String>(DashboardActivity.this,
R.layout.list_item, R.id.label, comments));
}// end if key is == 1
else {
// Error in registration
registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
}// end else
}// end if
} // end try
catch (final JSONException e) {
e.printStackTrace();
}// end catch
}
}
new loadComments().execute();
我的活动 xml 是这个
<RelativeLayout
android:id="@+id/dashboardRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:orientation="vertical" >
<Button
android:id="@+id/btnPost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dashboard_post"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:text="@string/post"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/commentSection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnPost"
android:layout_margin="20dip"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<!-- Error message -->
<TextView
android:id="@+id/collectComment_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/commentSection"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dip"
android:textColor="#e30000"
android:textStyle="bold" />
</RelativeLayout>