Coding TitlesFragment

We are really close to being able to actually show a photo in our Photos app. We need to load a list of photo titles from our database into ListView, provided by ListFragment, and handle what happens when the user clicks on a list item.

Some of the code in here won't make complete sense until we finish coding MainActivity, which will, of course, handle the communication between TitlesFragment and ViewFragment, as well as TagsFragment and TitlesFragment.

Let's add two new members to TitlesFragment—a Cursor member to load some data into and an instance of our new interface.

Add the two highlighted member variables where shown:

public class TitlesFragment extends ListFragment {

  private Cursor mCursor;
  private ActivityComs mActivityComs;

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

Next, in the onCreate method, we do the following:

  • Get a string to represent the tag from Bundle, using getArguments.getString to search for in the database.
  • Get an instance of DataManager.
  • If TitlesFragment receives a tag to search for, it will only show the photo titles with that matching tag, but if it receives no tag, it will show all the titles. With this in mind, the if block either loads the Cursor object up with data using getTitles or getTitlesWithTag.
  • Next, we create an instance of SimpleCursorAdapter. A cursor adapter is just like an array adapter, except it uses a Cursor object. SimpleCursorAdapter is perfect for displaying straightforward data in a ListView from Cursor. The slightly intimidating list of arguments its constructor takes is not as bad as it looks. The important arguments are a layout for each list item for ListView. list_item_1 is provided by default; we don't need to create it. Next is Cursor containing the data, following that is the way to identify the data within the cursor, and we pass the column name from the table.
  • Then, we call setListAdapter to set our new SimpleCursorAdapter as the adapter for ListView.

Tip

To learn more about SimpleCursorAdapter, check out the Android developer site: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html.

Add the following highlighted code to onCreate that we have just discussed:

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

  // Get the tag to search for
  String tag = getArguments().getString("Tag");

  // Get an instance of DataManager
  DataManager d = new DataManager(getActivity().getApplicationContext());


  if(tag == "_NO_TAG"){
    // Get all the titles from the database
    mCursor = d.getTitles();
  }else{
    // Get all the titles with a specific related tag
    mCursor = d.getTitlesWithTag(tag);
  }


  // Create a new adapter
  SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getActivity(),
  android.R.layout.simple_list_item_1, mCursor,
  new String[] { DataManager.TABLE_ROW_TITLE },
  new int[] { android.R.id.text1 }, 0 );

  // Attach the adapter to the ListView
  setListAdapter(cursorAdapter);
}

Now, we can handle what happens when the user taps on a title in ListView. We implement the onListItemClick method, and all we need is the position parameter of this method in order to do our work.

We use the moveToPosition method on our Cursor and pass in position to set the cursor to the right place for the next line of code.

We then declare and initialize an int variable called dBID with the value of the _id column from the database with this line of code:

int dBID = mCursor.getInt(
  mCursor.getColumnIndex(
  DataManager.TABLE_ROW_ID));

Finally, we can call the onTitlesListItemSelected method of our interface to pass the appropriate _id value to MainActivity.

Add the onListItemClick method we have just discussed to TitlesFragment:

public void onListItemClick(ListView l, View v, int position, long id) {

  // Move the cursor to the clicked item in the list
  mCursor.moveToPosition(position);

  // What is the database _id of this item?
  int dBID = mCursor.getInt(
    mCursor.getColumnIndex(
    DataManager.TABLE_ROW_ID));

  // Use the interface to send the clicked _id
  mActivityComs.onTitlesListItemSelected(dBID);
}

Of course, we haven't initialized our mActivityComs member yet, and we do so the same way we did in Chapter 19, Using Multiple Fragments, and set it to null the same way also.

Add the final two methods to achieve this at the appropriate times:

@Override
public void onAttach(Activity activity) {
  super.onAttach(activity);
  mActivityComs = (ActivityComs)activity;
}

@Override
public void onDetach() {
  super.onDetach();
  mActivityComs = null;
}

We can now code TagsFragment to communicate with TitlesFragment via MainActivity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.228.88