show tweets in a listview in twitter4j
I have been playing around with twitter4j and Android and so far so good.
However I am having problems figuring out how to display the tweets
properly on a list View. Here is my code so far, based on the 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);
}
});
}
It retrieves the last 20 tweets or so just fine. But my problems comes
fromt he final part, where I bind the list rawStatuses to the adapter and
the listView, because it basically prints ALL the information on screen:
Ok, not all that information is useful or ever needed, but it is there.
And I don't know how to display in the listView only the Twitter handle
and the tweet, for example (i.e. extract that information from the list
rawStatuses properly). I thought about having a List with the most useful
information (something like: List <User, Tweet, ID, Time> ), but it seems
to me cumbersome and a very poor solution.
My question is: How can or should manage all that information that a tweet
contains (an many tweets) so I can display what I want and still have the
rest? The closest answer I have found is this answer, but the link given
doesn't work anymore.
I hope I have explained myself. Thanks in advance.
No comments:
Post a Comment