Can't get .maxItemsPerRequest() to work in Android

android
fabric

#1

Hello all!

I can’t seem to get .maxItemsPerRequest() to work on my UserTimeline or SearchTimeline. I’ve probably made a very basic mistake, so please excuse my ignorance.

I’m trying to get my app to return the 3 latest tweets from my own timeline upon creation. There’s no ability to refresh tweets or anything special like that; just a static display in a ListView.
Here’s my MainActivity.java:

public class MainActivity extends ListActivity {

    // Note: Your consumer key and secret should be obfuscated in your source code before shipping.
    private static final String TWITTER_KEY = "********";
    private static final String TWITTER_SECRET = "********";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)
                findViewById(R.id.collapsing_toolbar);
        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            boolean isShow = false;
            int scrollRange = -1;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (scrollRange == -1) {
                    scrollRange = appBarLayout.getTotalScrollRange();
                }
                if (scrollRange + verticalOffset == 0) {
                    collapsingToolbar.setTitle("Morosphere");
                    isShow = true;
                } else if(isShow) {
                    collapsingToolbar.setTitle(" ");
                    isShow = false;
                }
            }
        });

        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new Twitter(authConfig));
        final UserTimeline userTimeline = new UserTimeline.Builder()
                .screenName("morosphere")
                .maxItemsPerRequest(3)
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
                .setTimeline(userTimeline)
                .build();

        setListAdapter(adapter);

    }
}

And here’s the relevant part of my XML:

<android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/twitter_card"

                style="@style/CardStyle"
                card_view:cardCornerRadius="16dp"
                card_view:cardBackgroundColor="#ffffff">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="5dp">

                    <TextView
                        style="@style/CardTextHeader"
                        android:drawableLeft="@drawable/ic_contact_twitter"
                        android:drawablePadding="5dp"
                        android:text="@string/twitter_card_header"/>

                    <View
                        style="@style/CardTitleDivider" />

                    <ListView
                        android:id='@android:id/list'
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                </LinearLayout>

            </android.support.v7.widget.CardView>

Regardless of what number I insert into .maxItemsPerRequest(), the UserTimeline always returns the maximum amount of Tweets.
What am I doing wrong?

Happy to provide any information not already in this post :slight_smile:


#2

Setting the max count doesn’t guarantee max items will actually be returned.

From https://dev.twitter.com/rest/reference/get/statuses/user_timeline

Specifies the number of Tweets to try and retrieve, up to a maximum of 200 per distinct request. The value of count is best thought of as a limit to the number of Tweets to return because suspended or deleted content is removed after the count has been applied. We include retweets in the count, even if include_rts is not supplied. It is recommended you always send include_rts=1 when using this API method.


#3

Perhaps I’m not understanding your reply, or maybe my question is phrased
poorly.

I am trying to get my app to display a maximum of 3 tweets by adding
.maxItemsPerRequest(3) to the UserTimeline builder.
No matter what number I put in there (3, 0, 100), the UserTimeline returns
my entire timeline (around 50 tweets).

Apologies if I’ve misunderstood your reply :frowning:


#4

Ok. I think I understand now. So maxItemsPerRequest just limits the number of items per request but the timeline will keep requesting Tweets until the view is filled. So you could try using the FixedTweetTimeline. Here we make a API call to userTimeline endpoint and add the top 3 Tweets to the FixedTweetTimeline.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Call<List<Tweet>> call = TwitterCore.getInstance().getApiClient().getStatusesService().userTimeline(userId,
                screenName, maxItemsPerRequest, sinceId, maxId, false, !includeReplies, null,
                includeRetweets);

        call.enqueue(new Callback<List<Tweet>>() {
            @Override
            public void success(Result<List<Tweet>> result) {
                final FixedTweetTimeline fixedTimeline = new FixedTweetTimeline.Builder()
                        .setTweets(result.data.subList(0, result.data.size() > 3 ? 3 : result.data.size())).build();
                final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(getActivity(),
                        fixedTimeline);
                setListAdapter(adapter);
            }

            @Override
            public void failure(TwitterException exception) {
                //show error
            }
        });
      }

#5

I can’t even begin to thank you enough for this! It works perfectly.
This has been annoying me for a couple of days, and it’s finally sorted!

Thanks so much :smiley: