Android Open Source - 2014-Droid-code Field Note List Fragment






From Project

Back to project page 2014-Droid-code.

License

The source code is released under:

GNU General Public License

If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.introtoandroid.simplefragments;
//ww  w.j  a  v  a2 s . c om

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FieldNoteListFragment extends ListFragment implements
    FragmentManager.OnBackStackChangedListener {

  int mCurPosition = -1;
  boolean mShowTwoFragments;
  private final static String TAG ="FRAG-LISTFRAGMENT";

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // Populate our ListView control within the Fragment
    String[] fieldNotes = getResources().getStringArray(
        R.array.fieldnotes_array);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_activated_1, fieldNotes));

    // Check which state we're in
    View detailsFrame = getActivity().findViewById(R.id.fieldentry);
    mShowTwoFragments = detailsFrame != null
        && detailsFrame.getVisibility() == View.VISIBLE;

    if (savedInstanceState != null) {
      mCurPosition = savedInstanceState.getInt("curChoice", 0);
    }

    if (mShowTwoFragments == true || mCurPosition != -1) {
      // Set the initial url to our default blog post, or the last one shown
      viewAnimalInfo(mCurPosition);
    }

    // monitor back stack changes to update list view
    getFragmentManager().addOnBackStackChangedListener(this);

  }

  @Override
  public void onBackStackChanged() {
    // update position
    FieldNoteWebViewFragment details = (FieldNoteWebViewFragment) getFragmentManager()
        .findFragmentById(R.id.fieldentry);
    if (details != null) {
      mCurPosition = details.getShownIndex();
      getListView().setItemChecked(mCurPosition, true);
      
      // if we're in single pane, then we need to switch forward to the viewer
      if (!mShowTwoFragments) {
        viewAnimalInfo(mCurPosition);
      }
    }
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurPosition);
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    viewAnimalInfo(position);
  }

  void viewAnimalInfo(int index) {
    mCurPosition = index;
    if (mShowTwoFragments == true) {
      // Check what fragment is currently shown, replace if needed.
      FieldNoteWebViewFragment details = (FieldNoteWebViewFragment) getFragmentManager()
          .findFragmentById(R.id.fieldentry);
      if (details == null || details.getShownIndex() != index) {

        // Make new fragment to show this selection.
        FieldNoteWebViewFragment newDetails = FieldNoteWebViewFragment
            .newInstance(index);

        // Execute a transaction, replacing any existing fragment
        // with this one inside the frame.
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fieldentry, newDetails);
        // Add this fragment instance to the back-stack of the Activity
        // so we can backtrack through our field notes
        if (index != -1) {
          String[] fieldNotes = getResources().getStringArray(
              R.array.fieldnotes_array);
          String strBackStackTagName = fieldNotes[index];
          ft.addToBackStack(strBackStackTagName);
        }
        // Fade between Urls
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
      }

    } else {
      // Otherwise we need to launch a new activity to display
      // the dialog fragment with selected text.
      Intent intent = new Intent();
      intent.setClass(getActivity(), FieldNoteViewActivity.class);
      intent.putExtra("index", index);
      startActivity(intent);
    }
  }

  @Override
  public void onAttach(Activity activity) {
    Log.d(TAG, "FRAGMENT onAttach()");
    super.onAttach(activity);
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "FRAGMENT onCreate()");
    super.onCreate(savedInstanceState);
  }

  @Override
  public void onDestroy() {
    Log.d(TAG, "FRAGMENT onDestroy()");
    super.onDestroy();
  }

  @Override
  public void onDetach() {
    Log.d(TAG, "FRAGMENT onDetach()");
    super.onDetach();
  }

  @Override
  public void onPause() {
    Log.d(TAG, "FRAGMENT onPause()");
    super.onPause();
  }

  @Override
  public void onResume() {
    Log.d(TAG, "FRAGMENT onResume(): "
        + mCurPosition);
    super.onResume();
  }

  @Override
  public void onStart() {
    Log.d(TAG, "FRAGMENT onStart()");
    super.onStart();
  }

  @Override
  public void onStop() {
    Log.d(TAG, "FRAGMENT onStop()");
    super.onStop();
  }

}




Java Source Code List

ca.campbell.httpexample.HttpExample.java
ca.campbell.httpexamplepost.HttpsExamplePOST.java
ca.campbell.layoutprogrammatically.MainActivity.java
ca.campbell.networkcheckstatus.MainActivity.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity3.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.simplegridview.MainActivity.java
ca.campbell.week2_rw_views.Activity2.java
ca.campbell.week2_rw_views.MainActivity.java
com.androidbook.simpleasync.ChoiceActivity.java
com.androidbook.simpleasync.SimpleAsyncActivity.java
com.androidbook.simpleasync.SimpleNoBGThread.java
com.androidbook.simpleasync.SimpleThreadActivity.java
com.cookbook.internet.search.GoogleSearch.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java
cs518.sample.activityLifecycle.Activity2.java
cs518.sample.activityLifecycle.MyActivityLifeCycleActivity.java
cs518.sample.database.AddStudent.java
cs518.sample.database.DBHelper.java
cs518.sample.database.DatabaseActivity.java
cs518.sample.database.Thirty.java
cs518.sample.dbcursoradapter.AddStudent.java
cs518.sample.dbcursoradapter.DBHelper.java
cs518.sample.dbcursoradapter.DatabaseActivity.java
cs518.sample.dbcursoradapter.Thirty.java
cs518.sample.localisation.MainActivity.java
cs518.sample.multiactivity.Activity1.java
cs518.sample.multiactivity.Activity2.java
cs518.sample.multiactivity.Activity3.java
cs518.sample.multiactivity.Activity4.java
cs518.sample.multiactivity.Activity5.java
cs518.sample.multiactivity.Activity6.java
cs518.sample.multiactivity.Constants.java
cs518.sample.usecalendarcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovidercursorloader2.MainActivity.java
cs518.sample.usemediastorecontentprovider.MainActivity.java
cs518.samples.imageswap.MainActivity.java
cs518.samples.sharedpreferences.MainActivity.java
cs534.sample.dbAsyncTask.AddStudent.java
cs534.sample.dbAsyncTask.DBHelper.java
cs534.sample.dbAsyncTask.DatabaseActivity.java
cs534.sample.dbAsyncTask.Thirty.java
cs534.sample.implicitintents.MainActivity.java
cs534.sample.multithread.MultiThread.java
cs534.sample.simplelistview.SimpleLV.java
cs534.samples.simplestlv.MainActivity.java