在Facebook onComplete上调用setAdapter

时间:2013-02-21 02:09:21

标签: android facebook-graph-api

我正在使用Facebook API,而我很难让ListView显示数据。我的代码使用片段并膨胀以ListView为根的布局。 ListView包含在另一个PullToRefreshListView中以启用Pull To Refresh。我也在包装适配器,所以它可以使用Mark Murphy的Endless Adapter。如果我在创建片段之前加载所有Facebook数据,一切正常。但我想用空ListView创建片段,并在异步获取Facebook数据后填充它。

我正在使用Facebook onComplete(Response response)中的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);
}

1 个答案:

答案 0 :(得分:0)

这是因为

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

此代码在执行之前执行:

feedList.setAdapter(endlessAdapter);
onComplete中的

。 因为你调用了initialFeedRequest.executeAsync();,这意味着请求是在UI-Thread旁边的另一个线程中执行的。你必须执行

refreshFeedList = (PullToRefreshListView) view.findViewById(R.id.feedList);
feedList = refreshFeedList.getRefreshableView();
feedList.setDividerHeight(0);
onComplete完成工作后

另一件事,你应该从onViewCreate()返回一个视图,因为它的返回类型是View

<强>编辑:

responseObject放入onCopmlete中的SharedPreference:

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

在其他条件下取回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);

如果你想使用Application类,那么看看: 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>