【发布时间】:2013-08-20 22:58:08
【问题描述】:
我一直在玩 twitter4j 和 Android,到目前为止一切都很好。但是,我在弄清楚如何在列表视图上正确显示推文时遇到了问题。到目前为止,这是我的代码,基于code example given on the Twitter4j website:
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,因为它基本上会在屏幕上打印所有信息:
好吧,并不是所有的信息都是有用的或永远需要的,但它就在那里。而且我不知道如何在 listView 中仅显示 Twitter 句柄和推文,例如(即正确地从列表 rawStatuses 中提取该信息)。我考虑过拥有一个包含最有用信息的列表(例如:
List <User, Tweet, ID, Time> ),但在我看来这很麻烦而且是一个非常糟糕的解决方案。
我的问题是:如何才能或应该管理推文包含的所有信息(许多推文),以便我可以显示我想要的内容并仍然拥有其余信息?我找到的最接近的答案是this answer,但给出的链接不再有效。
我希望我已经解释了自己。提前致谢。
【问题讨论】: