diff options
Diffstat (limited to 'Android/04-TheFragmentClass/FragmentsLab/src/course/labs')
4 files changed, 248 insertions, 0 deletions
diff --git a/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FeedFragment.java b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FeedFragment.java new file mode 100644 index 0000000..50562a1 --- /dev/null +++ b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FeedFragment.java @@ -0,0 +1,38 @@ +package course.labs.fragmentslab; + +import android.app.Fragment; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class FeedFragment extends Fragment { + + private static final String TAG = "Lab-Fragments"; + + private TextView mTextView; + private static FeedFragmentData feedFragmentData; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.feed, container, false); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + // Read in all Twitter feeds + if (null == feedFragmentData) { + feedFragmentData = new FeedFragmentData(getActivity()); + } + } + + // Display Twitter feed for selected feed + void updateFeedDisplay(int position) { + Log.i(TAG, "Entered updateFeedDisplay()"); + mTextView = (TextView) getView().findViewById(R.id.feed_view); + mTextView.setText(feedFragmentData.getFeed(position)); + } +} diff --git a/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java new file mode 100644 index 0000000..fb09cae --- /dev/null +++ b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FeedFragmentData.java @@ -0,0 +1,83 @@ +package course.labs.fragmentslab; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.content.Context; +import android.util.Log; +import android.util.SparseArray; + +// Utility class that provides stored Twitter feed data +public class FeedFragmentData { + private static final String TAG = "FeedFragmentData"; + private static final int[] IDS = { R.raw.ladygaga, R.raw.rebeccablack, R.raw.taylorswift }; + + private SparseArray<String> mFeeds = new SparseArray<String>(); + private Context mContext; + + + public FeedFragmentData(Context context) { + mContext = context; + loadFeeds(); + } + + // Load all stored Twitter feeds into the mFeeds SparseArray. + private void loadFeeds() { + for (int id : IDS) { + InputStream inputStream = mContext.getResources().openRawResource(id); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + StringBuffer buffer = new StringBuffer(""); + + // Read raw data from resource file + try { + String line = ""; + while ((line = reader.readLine()) != null) { + buffer.append(line); + } + } catch (IOException e) { + Log.i(TAG, "IOException"); + } + + // Convert raw data into a String + JSONArray feed = null; + try { + feed = new JSONArray(buffer.toString()); + } catch (JSONException e) { + Log.i(TAG, "JSONException"); + } + mFeeds.put(id, procFeed(feed)); + } + } + + // Convert JSON formatted data to a String + private String procFeed(JSONArray feed) { + String name = ""; + String tweet = ""; + + // string buffer for twitter feeds + StringBuffer textFeed = new StringBuffer(""); + + for (int j = 0; j < feed.length(); j++) { + try { + tweet = feed.getJSONObject(j).getString("text"); + JSONObject user = (JSONObject) feed.getJSONObject(j).get("user"); + name = user.getString("name"); + } catch (JSONException e) { + Log.i(TAG, "JSONException while processing feed"); + } + textFeed.append(name + " - " + tweet + "\n\n"); + } + return textFeed.toString(); + } + + // Return the Twitter feed data for the specified position as a single String + String getFeed (int position) { + return mFeeds.get(IDS[position]); + } +} diff --git a/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FriendsFragment.java b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FriendsFragment.java new file mode 100644 index 0000000..3d18ee6 --- /dev/null +++ b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/FriendsFragment.java @@ -0,0 +1,75 @@ +package course.labs.fragmentslab; + +import android.app.Activity; +import android.app.ListFragment; +import android.os.Build; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +public class FriendsFragment extends ListFragment { + + private static final String[] FRIENDS = { "ladygaga", "msrebeccablack", "taylorswift13" }; + private static final String TAG = "Lab-Fragments"; + + public interface SelectionListener { + public void onItemSelected(int position); + } + + private SelectionListener mCallback; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // use different layout definition, depending on whether device is pre- + // or post-honeycomb + int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; + // Set the list adapter for this ListFragment + setListAdapter(new ArrayAdapter<String>(getActivity(), layout, FRIENDS)); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + // Make sure that the hosting Activity has implemented + // the SelectionListener callback interface. We need this + // because when an item in this ListFragment is selected, + // the hosting Activity's onItemSelected() method will be called. + try { + mCallback = (SelectionListener) activity; + + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + " must implement SelectionListener"); + } + } + + // Note: ListFragments come with a default onCreateView() method. + // For other Fragments you'll normally implement this method. + // @Override + // public View onCreateView(LayoutInflater inflater, ViewGroup container, + // Bundle savedInstanceState) + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + Log.i(TAG, "Entered onActivityCreated()"); + // When using two-pane layout, configure the ListView to highlight the + // selected list item + if (isInTwoPaneMode()) { + getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); + } + } + + @Override + public void onListItemClick(ListView l, View view, int position, long id) { + // Notify the hosting Activity that a selection has been made. + mCallback.onItemSelected(position); + } + + // If there is a FeedFragment, then the layout is two-pane + private boolean isInTwoPaneMode() { + return getFragmentManager().findFragmentById(R.id.feed_frag) != null; + } +} diff --git a/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/MainActivity.java b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/MainActivity.java new file mode 100644 index 0000000..6309bae --- /dev/null +++ b/Android/04-TheFragmentClass/FragmentsLab/src/course/labs/fragmentslab/MainActivity.java @@ -0,0 +1,52 @@ +package course.labs.fragmentslab; + +import android.app.Activity; +import android.app.FragmentTransaction; +import android.os.Bundle; +import android.util.Log; + +public class MainActivity extends Activity implements +FriendsFragment.SelectionListener { + + private static final String TAG = "Lab-Fragments"; + + private FriendsFragment mFriendsFragment; + private FeedFragment mFeedFragment; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main_activity); + // If the layout is single-pane, create the FriendsFragment + // and add it to the Activity + if (!isInTwoPaneMode()) { + mFriendsFragment = new FriendsFragment(); + //TODO 1 - add the FriendsFragment to the fragment_container + } else { + // Otherwise, save a reference to the FeedFragment for later use + mFeedFragment = (FeedFragment) getFragmentManager().findFragmentById(R.id.feed_frag); + } + } + + // If there is no fragment_container ID, then the application is in + // two-pane mode + private boolean isInTwoPaneMode() { + return findViewById(R.id.fragment_container) == null; + } + + // Display selected Twitter feed + public void onItemSelected(int position) { + Log.i(TAG, "Entered onItemSelected(" + position + ")"); + // If there is no FeedFragment instance, then create one + if (mFeedFragment == null) + mFeedFragment = new FeedFragment(); + // If in single-pane mode, replace single visible Fragment + if (!isInTwoPaneMode()) { + //TODO 2 - replace the fragment_container with the FeedFragment + // execute transaction now + getFragmentManager().executePendingTransactions(); + } + // Update Twitter feed display on FriendFragment + mFeedFragment.updateFeedDisplay(position); + } +} |