Search code examples
androidlistviewtabsactionbarsherlock

How to use ListView with ActionBarSherlock?


I need to use ActionBarSherlock for my project but I am getting an error when using the ListView. I know I need to extend to ListActivity but I am sure there is an other way out of this. Below are my classes.

public class ViewPagerAdapter extends FragmentPagerAdapter {

// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int arg0) {
    switch (arg0) {

    // Open Maths.java
    case 0:
        MathActivity maths = new MathActivity();
        return maths;

        // Open Units.java
    case 1:
        UnitsActivity units = new UnitsActivity();
        return units;

        // Open Physics.java
    case 2:
        PhysicsActivity physics = new PhysicsActivity();
        return physics;

    }
    return null;
}

Below is my MathActivity Class which extends the SherlockFragment.

public class MathActivity extends SherlockFragment {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.math_layout);

    String[] mathList = new String[] {"Item 1", "Item 2", "Item 3", "Item 4"};
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mathList));


}

Solution

  • If you want a ListView, you should probably be extending SherlockListFragment, not just SherlockFragment. (Or you need to explicitly find your list and set its listadapter, not try and set the listadapter on the containing fragment.)

    Your ListView in your XML needs to have an id of @android:id/list, e.g. mathlist.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      >
      <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
          />
    </LinearLayout>
    

    And then something like this:

    public class MathActivity extends SherlockListFragment implements OnItemClickListener
    {
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState)
      {
        View view = inflater.inflate(R.layout.mathlist, null);
        String[] mathList = new String[] {"Item 1", "Item 2", "Item 3", "Item 4"};
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mathList));
        return view;
      }
    
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
      {
        // handle listview item clicks in here    
      }
    
      // Don't let items be clicked until the activity has finished drawing everything:
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView().setOnItemClickListener(this);
      }
    }