我正在使用Facebook API,我很難讓ListView
顯示數據。我的代碼使用片段並膨脹一個以ListView
爲根的佈局。 ListView
被封裝在另一個PullToRefreshListView
中以啓用Pull To Refresh。我也在包裝適配器,因此它可以使用Mark Murphy的Endless Adapter。如果我在創建片段之前加載所有Facebook數據,那麼一切正常。但是我想創建一個空ListView
的片段,並在我異步獲取Facebook數據時填充它。在Facebook上調用setAdapter onComplete
我使用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);
}
我是你的片段代碼嗎?當從onCompleted()調用setAdapter時,您觀察到了什麼?只是空的清單? – sandrstar 2013-02-21 04:46:23
當我從onCompleted()調用setAdapter時,我得到一個空列表。但是如果我切換到另一個片段並返回到這個片段,那麼這個列表填充就好了。我改變了else語句中的內容與catch JSONException之後的行相同。所以,即使JSONArray在onCompleted()中加載,看起來像我不能用同樣的方法調用setAdapter。 – Wenger 2013-02-21 11:44:49