使用结构和Twitter SDK可以在ListView
内获得用户时间线,因为它正是我的应用程序所需要的(我喜欢格式化和简单)。
这是我的测试代码。我通过推特ID在ListView
内收到推文。
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;
}
}
答案 0 :(得分: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;
}
}
);
}
此示例非常有用:cannonball app