Search code examples
androidlistviewtwittertwitter-fabric

Twitter user timeline in ListView by Fabric


It's possible to get a user timeline inside of a ListView using fabric and the Twitter SDK because its exactly what I need for my App (I like the formatting and its simple).

Here is my testing code. I get tweets inside the ListView by tweet ID.

package com.example.elfassimounir.happh;

import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetui.CompactTweetView;
import com.twitter.sdk.android.tweetui.LoadCallback;
import com.twitter.sdk.android.tweetui.TweetUtils;
import com.twitter.sdk.android.tweetui.TweetViewFetchAdapter;
import java.util.Arrays;
import java.util.List;
import io.fabric.sdk.android.Fabric;

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 = "xxxxxxxx";
private static final String TWITTER_SECRET = "xxxxxxxx";

List<Long> tweetIds = Arrays.asList(503435417459249153L,
        510908133917487104L,
        473514864153870337L,
        477788140900347904L,
        510908133917487104L,
        473514864153870337L);
final TweetViewFetchAdapter adapter =
        new TweetViewFetchAdapter<CompactTweetView>(
                MainActivity.this);

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    final TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);

    Fabric.with(this, new Twitter(authConfig));
    setContentView(R.layout.tweet_list);
    setListAdapter(adapter);
    adapter.setTweetIds(tweetIds,
            new LoadCallback<List<Tweet>>() {
                @Override
                public void success(List<Tweet> tweets) {
                    // my custom actions
                }
                @Override
                public void failure(TwitterException exception) {
                    // Toast.makeText(...).show();
                }
            });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

}

Solution

  • Finally i have an answer for my question thanks to twitter fabric team.

    This code is searching for the user i need his tweets:

    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 = "XXXXX";
    private static final String TWITTER_SECRET = "XXXXX";
    
    private boolean flagLoading;
    private boolean endOfSearchResults;
    private static final String SEARCH_QUERY = "NFL";
    private TweetViewAdapter adapter;
    private static final String SEARCH_RESULT_TYPE = "recent";
    private static final int SEARCH_COUNT = 20;
    private long maxId;
    
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tweet_list);
    
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new TwitterCore(authConfig), new TweetUi(), new Twitter(authConfig), new TweetUi());
    
        setUpViews();
    
        loadTweets();
    }
    
    private void setUpViews() {
       // setUpBack();
        setUpPopularList();
    }
    
    private void setUpPopularList() {
        adapter = new TweetViewAdapter(MainActivity.this);
        setListAdapter(adapter);
    
        getListView().setEmptyView(findViewById(R.id.loading));
        getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {}
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount == totalItemCount) &&
                        totalItemCount != 0) {
                    Crashlytics.log("Bla bla..");
                    if (!flagLoading && !endOfSearchResults) {
                        flagLoading = true;
                        loadTweets();
                    }
                }
            }
        });
    }
    private void loadTweets() {
        Crashlytics.log("Bla bla...");
        setProgressBarIndeterminateVisibility(true);
    
        final SearchService service = Twitter.getApiClient().getSearchService();
        service.tweets(SEARCH_QUERY, null, null, null, SEARCH_RESULT_TYPE, SEARCH_COUNT, null, null,
                maxId, true, new Callback<Search>() {
                    @Override
                    public void success(Result<Search> searchResult) {
    
                        setProgressBarIndeterminateVisibility(false);
                        final List<Tweet> tweets = searchResult.data.tweets;
                        adapter.getTweets().addAll(tweets);
                        adapter.notifyDataSetChanged();
                        if (tweets.size() > 0) {
                            maxId = tweets.get(tweets.size() - 1).id - 1;
                        } else {
                            endOfSearchResults = true;
                        }
                        flagLoading = false;
                    }
    
                    @Override
                    public void failure(TwitterException error) {
                        Crashlytics.logException(error);
    
                        setProgressBarIndeterminateVisibility(false);
                        Toast.makeText(MainActivity.this,
                                getResources().getString(R.string.toast_retrieve_tweets_error),
                                Toast.LENGTH_SHORT).show();
    
                        flagLoading = false;
                    }
                }
        );
    }
    

    this example is very helpful : cannonball app