gpt4 book ai didi

android - Fabric ListView 中的 Twitter 用户时间线

转载 作者:行者123 更新时间:2023-11-29 01:36:43 25 4
gpt4 key购买 nike

可以使用 fabric 和 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;
}

}

最佳答案

感谢 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

关于android - Fabric ListView 中的 Twitter 用户时间线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com