我正在使用 Facebook API,但很难获得ListView
显示数据。我的代码使用片段并膨胀以 aListView
作为根的布局。它ListView
被包裹在另一个PullToRefreshListView
中以启用Pull To Refresh。我还包装了适配器,以便它可以使用 Mark Murphy 的Endless Adapter。如果我在创建片段之前加载所有 Facebook 数据,一切正常。但是我想创建一个空的片段,ListView
并在异步获取 Facebook 数据后填充它。
我onComplete(Response response)
在 Facebook 中使用了回调方法,但由于某种原因Request
,我似乎无法setAdapter(adapter)
在该方法之外使用。onCreateView()
我觉得我只是错过了一些非常简单的东西。这是代码:
JSONArray newsFeed;
View view;
ListView feedList;
PullToRefreshListView refreshFeedList;
Request initialFeedRequest = new Request(session, "me/home", null, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
Log.i("NewsFeed", "initial load onComplete");
Log.i("NewsFeed",response.toString());
responseObject = response.getGraphObject().getInnerJSONObject();
try {
newsFeed = responseObject.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
feedAdapter = new FeedAdapter(getActivity(), Space.newsFeed);
endlessAdapter = new EndlessFeed(feedAdapter);
feedList.setAdapter(endlessAdapter);
refreshFeedList.setOnRefreshListener(refreshListener);
nextPageRequest = response.getRequestForPagedResults(PagingDirection.NEXT);
String test = feedAdapter.getItem(0).toString();
Log.i("NewsFeed", "First Item: " + test);
}
});
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.news_feed, container, false);
if(feedAdapter==null){
Log.i("NewsFeed", "executing Request");
initialFeedRequest.executeAsync();
}
else{
feedAdapter = new FeedAdapter(getActivity(), Space.newsFeed);
endlessAdapter = new EndlessFeed(feedAdapter);
feedList.setAdapter(endlessAdapter);
refreshFeedList.setOnRefreshListener(refreshListener);
}
refreshFeedList = (PullToRefreshListView) view.findViewById(R.id.feedList);
feedList = refreshFeedList.getRefreshableView();
feedList.setDividerHeight(0);
}