我有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>
答案 0 :(得分:0)
您是否尝试过为列表适配器调用notifyDataSetChanged
?
ArrayAdapter adapter = new ArrayAdapter<String>(DashboardActivity.this,
R.layout.list_item, R.id.label, comments)
setListAdapter(adapter);
adapter.notifyDataSetChanged;