2013-02-21 54 views
0

我正在使用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); 
} 
+0

我是你的片段代碼嗎?當從onCompleted()調用setAdapter時,您觀察到了什麼?只是空的清單? – sandrstar 2013-02-21 04:46:23

+0

當我從onCompleted()調用setAdapter時,我得到一個空列表。但是如果我切換到另一個片段並返回到這個片段,那麼這個列表填充就好了。我改變了else語句中的內容與catch JSONException之後的行相同。所以,即使JSONArray在onCompleted()中加載,看起來像我不能用同樣的方法調用setAdapter。 – Wenger 2013-02-21 11:44:49

回答

0

發生這種情況,因爲

refreshFeedList = (PullToRefreshListView) view.findViewById(R.id.feedList); 
feedList = refreshFeedList.getRefreshableView(); 
feedList.setDividerHeight(0); 

這段代碼前執行:

feedList.setAdapter(endlessAdapter); 
中的onComplete

。 因爲你叫initialFeedRequest.executeAsync();,這意味着請求在UI-Thread旁邊的另一個線程中執行。 u必須執行

refreshFeedList = (PullToRefreshListView) view.findViewById(R.id.feedList); 
feedList = refreshFeedList.getRefreshableView(); 
feedList.setDividerHeight(0); 

後進行onComplete其工作

還有一件事,ü應該從onViewCreate()返回一個觀點,因爲它的返回類型爲View

編輯:

放您在SharedPreference中的responseObjectonCopmlete

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("responseObject", responseObject.toString()); 
editor.commit(); 

在其他條件getback您responseObject

JSONObject responseObject; 
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
String responseObjectString = prefs.getString("responseObject"); 
if(responseObjectString != null) 
    responseObject = new JSONObject(responseObjectString); 
newsFeed = responseObject.getJSONArray("data"); 

feedAdapter = new FeedAdapter(getActivity(), newsFeed); // use the newsFeed we get from SP 
endlessAdapter = new EndlessFeed(feedAdapter); 
feedList.setAdapter(endlessAdapter); 
refreshFeedList.setOnRefreshListener(refreshListener); 

如果u想使用的應用類,然後看到: Application class tutorial

然後我只好也改變您的清單:

<application 
    android:label="@string/app_name" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:name=".Your_Application_class_name"> 
// other components. .... 

</application> 
+0

謝謝。當我回家時,我會放棄它。這就說得通了。我想我會返回視圖,我只是沒有複製onCreateView()的整個代碼。 – Wenger 2013-02-21 15:31:47

+0

它仍然給我這些變化的同樣的事情。在調用setAdapter之前,我必須膨脹一個ListView嗎? – Wenger 2013-02-22 02:42:26

+0

是的,你可以在調用setAdapter之前給ListView充氣 – Shoshi 2013-02-22 02:45:52

相關問題