0

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

1 回答 1

0

这是因为

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

此代码在之前执行:

feedList.setAdapter(endlessAdapter);

在完成。因为你已经调用initialFeedRequest.executeAsync();了,这意味着请求在 UI-Thread 旁边的不同线程中执行。你必须执行

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

onComplete完成工作后

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

编辑:

将您responseObject的 SharedPreference 放入onCopmlete

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 
   android:label="@string/app_name"
   android:allowBackup="true"
   android:icon="@drawable/ic_launcher"
   android:name=".Your_Application_class_name">
// other components. ....

</application>
于 2013-02-21T12:03:24.937 回答