gpt4 book ai didi

java - Asynctask 中的基本适配器不起作用

转载 作者:行者123 更新时间:2023-11-29 20:58:39 24 4
gpt4 key购买 nike

我正在为我的项目使用 facebook 应用程序,我正在从图形 API 获取一个 json。我有一个带有 hashMap 的自定义 listView,但是当我运行时,列表不会填充但没有任何错误。请帮助我。

代码如下:

public class PageFeedHome extends Fragment {

ArrayList<HashMap<String, String>> feedList;
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_MESSAGE = "message";
private String feedMessage;
ListView listView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.feed_home_activity,
container, false);

listView = (ListView) view.findViewById(R.id.feed_lv);
feedList = new ArrayList<HashMap<String, String>>();

new LoadPosts().execute();

BaseAdapter adapter = new SimpleAdapter(getActivity(), feedList,
R.layout.feed_item_view, new String[] { TAG_MESSAGE, TAG_NAME,
TAG_ID }, new int[] { R.id.message, R.id.author,
R.id.id_tv });

listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

return view;
}

private class LoadPosts extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

Session.openActiveSession(getActivity(), true,
new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {

new Request(session, "/163340583656/feed",
null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(
Response response) {
/* handle the result */
Log.i("PostFeedResponse", response.toString());
try {
GraphObject graphObj = response
.getGraphObject();
JSONObject json = graphObj
.getInnerJSONObject();
JSONArray jArray = json
.getJSONArray("data");
for (int i = 0; i < jArray.length(); i++) {
JSONObject currObj = jArray.getJSONObject(i);
final String feedId = currObj.getString("id");
if (currObj.has("message")) {
feedMessage = currObj.getString("message");
} else if (currObj.has("story")) {
feedMessage = currObj.getString("story");
} else {
feedMessage = "Posted a something";
}
JSONObject fromObj = currObj.getJSONObject("from");
String from = fromObj.getString("name");

HashMap<String, String> feed = new HashMap<String, String>();
feed.put(TAG_ID, feedId);
feed.put(TAG_MESSAGE, feedMessage);
feed.put(TAG_NAME, from);

feedList.add(feed);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).executeAsync();

}
}
});

return null;
}

}

}

最佳答案

移动这段代码:

BaseAdapter adapter = new SimpleAdapter(getActivity(), feedList,
R.layout.feed_item_view, new String[] { TAG_MESSAGE, TAG_NAME,
TAG_ID }, new int[] { R.id.message, R.id.author,
R.id.id_tv }); //initialize adapter

listView.setAdapter(adapter); //set adapter
adapter.notifyDataSetChanged();

AsyncTask 的 onPostExecute 以确保 feedList 已从 onBackground 插入。

或者,如果您更喜欢初始化适配器并将适配器设置为onCreate 中的listview,只需移动adapter.notifyDataSetChanged();onPostExecute。如果您多次调用 AsyncTask,建议这样做,因为不需要多次初始化和设置适配器。 (请参阅我在代码中的评论)

关于java - Asynctask 中的基本适配器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26843167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com