Created
February 8, 2014 06:11
-
-
Save koalahamlet/8877379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mikes.Twitter.app.fragments; | |
import java.util.ArrayList; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Toast; | |
import com.mikes.Twitter.app.R; | |
import com.mikes.Twitter.app.TimelineActivity; | |
import com.mikes.Twitter.app.TweetsAdapter; | |
import com.mikes.Twitter.app.models.Tweet; | |
import eu.erikw.PullToRefreshListView; | |
public class TweetsListFragment extends Fragment { | |
ArrayList<Tweet> tweets = new ArrayList<Tweet>(); | |
protected PullToRefreshListView lvTweets; | |
TweetsAdapter adapter; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup parent, | |
Bundle savedInstanceState) { | |
//where you inflate xml | |
return inflater.inflate(R.layout.fragment_tweets_list, parent, false); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
//whenever fragment is displayed AND activity exists | |
super.onActivityCreated(savedInstanceState); | |
// Log.d("DEBUG", jsonTweets.toString()); | |
adapter = new TweetsAdapter(getActivity(), tweets); | |
lvTweets = (PullToRefreshListView) getActivity().findViewById(R.id.lvTweets); | |
lvTweets.setAdapter(adapter); | |
lvTweets.onRefreshComplete(); | |
} | |
public void showFailMessage() { | |
// TODO Auto-generated method stub | |
Toast.makeText(getActivity(), "Whoops, we can't seem to speak to teh netz at the moment. You'll see your tweets again once you've gotten a better connection.", Toast.LENGTH_LONG).show(); | |
} | |
public TweetsAdapter getAdapter() { | |
return adapter; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mikes.Twitter.app.fragments; | |
import java.util.List; | |
import org.json.JSONArray; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.loopj.android.http.JsonHttpResponseHandler; | |
import com.mikes.Twitter.app.EndlessScrollListener; | |
import com.mikes.Twitter.app.MyTwitterApp; | |
import com.mikes.Twitter.app.R; | |
import com.mikes.Twitter.app.TweetsAdapter; | |
import com.mikes.Twitter.app.models.Tweet; | |
import eu.erikw.PullToRefreshListView; | |
public class HomeTimelineFragment extends TweetsListFragment { | |
TweetsAdapter adapter; | |
List<Tweet> tweets; | |
TweetsListFragment fragmentTweets; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
MyTwitterApp.getRestClient().getHomeTimeline( | |
new JsonHttpResponseHandler() { | |
@Override | |
public void onSuccess(JSONArray jsonTweets) { | |
tweets = Tweet.fromJson(jsonTweets); | |
getAdapter().addAll(tweets); | |
// breaky breaky active android | |
// ActiveAndroid.beginTransaction(); | |
// try{ | |
// for (Tweet tweet : tweets) { | |
// Log.d("DEBUG", "User: "+ tweet.getUser().toString()); | |
// Log.d("DEBUG", "Tweet: " + tweet.toString()); | |
// tweet.getUser().save(); | |
// tweet.save(); | |
// } | |
// ActiveAndroid.setTransactionSuccessful(); | |
// } finally { | |
// ActiveAndroid.endTransaction(); | |
// } | |
} | |
@Override | |
public void onFailure(Throwable arg0, String arg1) { | |
// setProgressBarIndeterminateVisibility(Boolean.FALSE); | |
// showFailMessage(); | |
super.onFailure(arg0, arg1); | |
} | |
}); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
if(getView()==null){ | |
Log.d("hey", "dumbass"); | |
} | |
final PullToRefreshListView lvTweets = (PullToRefreshListView) getView().findViewById(R.id.lvTweets); | |
lvTweets.setOnScrollListener(new EndlessScrollListener() { | |
@Override | |
public void onLoadMore(int page, int totalItemsCount) { | |
// setProgressBarIndeterminateVisibility(Boolean.TRUE); | |
Tweet lastTweet = (Tweet) lvTweets.getItemAtPosition(totalItemsCount - 1); | |
// // minus one in the next call to get rid of duplicate tweet | |
MyTwitterApp.getRestClient().getMoreHomeTimeline(lastTweet.getTweetId() - 1, | |
new JsonHttpResponseHandler() { | |
@Override | |
public void onSuccess(JSONArray jsonTweets) { | |
// setProgressBarIndeterminateVisibility(Boolean.FALSE); | |
// Log.d("DEBUG", jsonTweets.toString()); | |
// tweets = Tweet.fromJson(jsonTweets); | |
// adapter.addAll(tweets); | |
// adapter.notifyDataSetChanged(); | |
// ActiveAndroid.beginTransaction(); | |
// try { | |
// for (Tweet tweetInstance : tweets) { | |
// tweetInstance.getUser().save(); | |
// tweetInstance.save(); | |
// } | |
// ActiveAndroid.setTransactionSuccessful(); | |
// } finally { | |
// ActiveAndroid.endTransaction(); | |
// } | |
} | |
@Override | |
public void onFailure(Throwable arg0, String arg1) { | |
showFailMessage(); | |
// setProgressBarIndeterminateVisibility(Boolean.FALSE); | |
super.onFailure(arg0, arg1); | |
} | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment