[英]show tweets in a listview in twitter4j
到目前為止,我一直在使用twitter4j和Android。 但是我在弄清楚如何在列表視圖上正確顯示推文時遇到問題。 到目前為止,這是我的代碼,基於Twitter4j網站上給出的代碼示例 :
public class MainActivity extends Activity {
//ListView with the tweets
private ListView timelineListView;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
//Adapter
ArrayAdapter<twitter4j.Status> tweetAdapter ;
//List
List<Status> rawStatuses;
//Other stuff
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
//Login methods, checking that there is an Internet connection, etc... When a button is
//pressed, an Asynctask is called and it retrieves the tweets:
class updateTimeline extends AsyncTask <Void, Void, Void>{
protected void onPreExecute(Void thing){
}
protected Void doInBackground(Void... arg0) {
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
// Access Token
String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token, access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
User user = twitter.verifyCredentials();
rawStatuses = twitter.getHomeTimeline();
System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
for (twitter4j.Status status : rawStatuses) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
//System.exit(-1);
}
return null;
}
protected void onPostExecute(Void result) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Timeline updated", Toast.LENGTH_SHORT)
.show();
tweetAdapter = new ArrayAdapter<twitter4j.Status>(MainActivity.this, android.R.layout.simple_list_item_1, rawStatuses);
timelineListView.setAdapter(tweetAdapter);
}
});
}
它檢索最后20條左右的推文就可以了。 但是我的問題來自最后一部分,我將列表rawStatuses綁定到適配器和listView,因為它基本上將所有信息打印在屏幕上:
好的,並不是所有的信息都是有用的或不需要的,但是它就在那里。 而且我不知道如何在列表中顯示,例如僅查看Twitter句柄和推文(例如,從列表rawStatuses中正確提取該信息)。 我曾考慮過使用一個包含最有用信息的List <User, Tweet, ID, Time>
(例如: List <User, Tweet, ID, Time>
),但是在我看來,這很麻煩且解決方案很差。
我的問題是:如何或應該管理一條tweet包含的所有信息(許多tweet),以便我可以顯示我想要的內容並保留其余信息? 我找到的最接近的答案是這個答案 ,但是給出的鏈接不再起作用。
我希望我已經解釋了自己。 提前致謝。
我遇到了同樣的問題,實際上您在正確的道路上。 為了解決這個問題,我還創建了一個字符串列表,並將其存儲到ArrayList中,然后將該列表放入ArrayAdapter中,而不是原始狀態:
ArrayAdapter<String> stringTweetAdapter;
List<twitter4j.Status> statuses;
List<String> stringStatuses = new ArrayList<String>();
.....
User user = twitter.verifyCredentials();
statuses = twitter.getHomeTimeline();
System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
for (twitter4j.Status status : statuses) {
Log.d("Twitter","@" + status.getUser().getScreenName() + " - " + status.getText());
String myTweets = ("@" + status.getUser().getScreenName() + " - " + status.getText());
stringStatuses.add(myTweets);
}
......
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
progressDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Retrieving TimeLine Done..", Toast.LENGTH_SHORT).show();
stringTweetAdapter = new ArrayAdapter<String>(ShowTimeline.this, R.layout.timeline_list_item, R.id.textTimelineItem, stringStatuses);
twitterFeed.setAdapter(stringTweetAdapter);
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.