WarnerYoung
WarnerYoung

Reputation: 233

How to use android support library correctly

I'm working my way through Professional Android 4 Application Development. Chapter 4 modifies the To Do List app to use fragments, but I'm trying to test on a Gingerbread device. There's mention in the book of using support libraries to allow using Android v3 or v4 features on a lower version device, but it's not covered very well.

I'm running into a problem specifically with:

    // Get references to the Fragments
    android.app.FragmentManager fm = getFragmentManager();
    ToDoListFragment    todoListFragment = (ToDoListFragment) fm.findFragmentById( R.id.ToDoListFragment );

I've got these imports at the top: import android.support.v4.app.FragmentManager; import android.support.v4.app.ListFragment;

But lint warns on the "ToDoListFragment todoListFragment = (ToDoListFragment)" line: cannot cast from Fragment to ToDoListFragment

In my ToDoListFragment class, I have:

    import android.support.v4.app.ListFragment;

    public class ToDoListFragment extends ListFragment {
    }

This is almost verbatim from the book, except for the change to use support library.

I'm not clear on how to get this code to work correctly using the v4 support library. I apologize in advance if this isn't enough info. I'm still learning this, and I'm more familiar with C/C++ than Java. If I don't use the support library, the code builds just fine and will run on an Ice Cream Sandwich device, but I'd like to get it working on lower level devices, too.

Upvotes: 8

Views: 10849

Answers (2)

Chris Johnson
Chris Johnson

Reputation: 131

I wanted to do the same with this example. There are several places where alterations are required to make it work with the support library. Here's my complete java file with the changes highlighted in the comments:

package com.paad.todolist;

import java.util.ArrayList;
import android.support.v4.app.FragmentActivity; // Because we're using the support library 
                                                // version of fragments, the import has to be
                                                // FragmentActivity rather than Activity
import android.support.v4.app.FragmentManager;  // Support version of Fragment Manager
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

// because we're using the support library version of fragments, the class has to extend the
// FragmentActivity superclass rather than the more usual Activity superclass
public class ToDoListActivity extends FragmentActivity implements NewItemFragment.OnNewItemAddedListener {

    // logging tag
    private static final String TAG = "ToDoListActivity";

    // create an array adaptor ready to bind the array to the list view
    private ArrayAdapter<String> aa;

    // set up array list to hold the ToDo items
    private ArrayList<String> todoItems;

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

        Log.i(TAG, "The onCreate method has started");

        // inflate the view
        setContentView(R.layout.activity_to_do_list);

        // get references to the fragments

        // FragmentManager fm = getFragmentManager(); this won't work with the support library version
        FragmentManager fm = getSupportFragmentManager();   // this is the equivalent for support library

        ToDoListFragment todoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.ToDoListFragment);

        // Create the array list of to do items
        todoItems = new ArrayList<String>();

        // Create the array adapter to bind the array to the listview
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);

        // bind the array adapter to the list view
        todoListFragment.setListAdapter(aa);        
    }

    // implement the listener... It adds the received string to the array list
    // then notifies the array adapter of the dataset change
    public void onNewItemAdded(String newItem) {
        todoItems.add(newItem);
        aa.notifyDataSetChanged();
    }           
}

Upvotes: 2

Alexander
Alexander

Reputation: 48252

You should use getSupportFragmentManager() instead of getFragmentManager()

android.support.v4.app.FragmentManager fm = getSupportFragmentManager()

Upvotes: 13

Related Questions