Back to project page SimpleTwitterClient.
The source code is released under:
MIT License
If you think the Android project SimpleTwitterClient listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.nickrasband.simpletwitterclient; /*from w w w .j a v a2s .c o m*/ import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import com.loopj.android.http.JsonHttpResponseHandler; import com.nickrasband.simpletwitterclient.models.Tweet; import com.nickrasband.simpletwitterclient.models.User; import eu.erikw.PullToRefreshListView; import eu.erikw.PullToRefreshListView.OnRefreshListener; public class TimelineActivity extends Activity { private static final int TWEETS_PER_LOAD = 25; private ArrayList<Tweet> tweets; private TweetsAdapter adapter; private PullToRefreshListView lvTweets; private User currentUser; private long smallest_tweet_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timeline); tweets = new ArrayList<Tweet>(); adapter = new TweetsAdapter(this, tweets); lvTweets = (PullToRefreshListView)findViewById(R.id.lvTweets); lvTweets.setAdapter(adapter); // Load initial 25 tweets. loadTweets(true); lvTweets.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { loadTweets(true); } }); lvTweets.setOnScrollListener(new EndlessScrollListener() { @Override public void onLoadMore(int page, int totalItemsCount) { // Triggered only when new data needs to be appended to the list // Add whatever code is needed to append new items to your AdapterView loadTweets(false); } }); } private void loadTweets(final Boolean freshList) { long smallest_tweet = smallest_tweet_id - 1; if (freshList) { smallest_tweet = 0; } SimpleTwitterClientApp.getRestClient().getHomeTimeline(TWEETS_PER_LOAD, smallest_tweet, new JsonHttpResponseHandler() { @Override public void onSuccess(JSONArray jsonTweetsArray) { lvTweets.onRefreshComplete(); // Clear out the old adapter, if necessary. if (freshList) { adapter.clear(); } adapter.addAll(Tweet.tweetsFromJsonArray(jsonTweetsArray)); // Only update lowest tweet id if it is valid. int index = adapter.getCount() - 1; if (index >= 0) { long tweet_id = adapter.getItem(index).getTweetId(); if (tweet_id != 0) { smallest_tweet_id = tweet_id; } } } public void onFailure(Throwable e, JSONObject error) { Log.e("ERROR", e.toString()); Log.e("ERROR", error.toString()); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.timeline, menu); return true; } // Compose a tweet when the compose menu // item is tapped. public void composeTweet(MenuItem item) { if (currentUser == null) { /* // First, get some user data, so that we can easily update user tweets. SimpleTwitterClientApp.getRestClient().getUserCredentials(true, false, new JsonHttpResponseHandler() { public void onSuccess(JSONObject user) { currentUser = new User(user); Intent i = new Intent(getBaseContext(), ComposeTweetActivity.class); i.putExtra("currentUser", currentUser); startActivityForResult(i, 0); } public void onFailure(Throwable e, JSONObject error) { Log.d("USER_CREDENTIALS", "Failed to get user credentials. Using empty currentUser object."); currentUser = new User(new JSONObject()); Intent i = new Intent(getBaseContext(), ComposeTweetActivity.class); i.putExtra("currentUser", currentUser); startActivityForResult(i, 0); } }); */ currentUser = new User(new JSONObject()); } Intent i = new Intent(this, ComposeTweetActivity.class); i.putExtra("current_user", currentUser); startActivityForResult(i, 0); } // Handle the result of the ComposeTweetActivity. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Result from the ComposeTweetActivity. if (requestCode == 0) { if (resultCode == RESULT_OK) { String tweetMessage = data.getStringExtra("message"); String tweetTimestamp = data.getStringExtra("tweet_timestamp"); String tweetId = data.getStringExtra("tweet_id"); currentUser = (User)data.getParcelableExtra("current_user"); addTweetForCurrentUser(tweetMessage, tweetTimestamp, tweetId); } } } private void addTweetForCurrentUser(String message, String tweetTimestamp, String tweetId) { adapter.insert(new Tweet(currentUser, message, tweetTimestamp, tweetId), 0); } }