Twitter UserTimeline works but SearchTimeline neve

2019-03-21 10:29发布

问题:

I am using the exact example code in the documents and yet I am getting no tweets at all for SearchTimeline, i.e.

No Tweet

If I replace with UserTimeline I got results, but of course I need SearchTimeline for my use case. Has anyone managed to get SearchTimeline to work? Also (fyi) I have Fabric setup for my app and am able to login users with their twitter accounts.

The exact code, which is as from document, which fails:

public class SearchTimelineFragment extends ListFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final SearchTimeline searchTimeline = new SearchTimeline.Builder()
                .query("#twitterflock")
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(getActivity())
                .setTimeline(searchTimeline)
                .build();
        setListAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.timeline, container, false);
    }
}

Also I am in development with no PlayStore presence, in case that matters.

FURTHERMORE

Adding callback block results in neither failure nor success message. It's as if the code is entirely not executed: no server call. But yet, onCreate is called. And again if I replace with UserTimeline I get results.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        final SearchTimeline searchTimeline = new SearchTimeline.Builder()
                .query("#twitterflock")
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(getActivity())
                .setTimeline(searchTimeline)
                .setOnActionCallback(new Callback<Tweet>() {
                    @Override
                    public void success(Result<Tweet> result) {
                        Log.d(TAG, "SearchTimeline successful");
                    }

                    @Override
                    public void failure(TwitterException e) {
                        Log.d(TAG, "SearchTimeline failed");
                        e.printStackTrace();
                        ;
                    }
                })
                .build();
        setListAdapter(adapter);
    }

Also I am using android.support.v4.app.ListFragment;

回答1:

That is because you are setting the list adapter in onCreate, while onCreateView is called after onCreate.

Set the listAdapter in OnCreateView.