2

可以在使用结构和 Twitter SDK 中获取用户时间线,ListView因为它正是我的应用程序所需要的(我喜欢格式和它的简单)。

这是我的测试代码。我在推ListView文 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;
}

}
4

1 回答 1

1

多亏了 twitter 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 = "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;
                }
            }
    );
}

这个例子很有帮助:炮弹应用

于 2015-02-03T20:47:07.360 回答