Search code examples
androidandroid-listfragment

How to use OnListItemClick action to open a new ListFragment?


I have the following ListItem listener, which I want when I click in one of the list open up another list with different options.

public class MyList extends ListFragment {

    ...

    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        switch (position) {
        case 0:
            Intent newActivity = new Intent(v.getContext(), AnotherList.class);
            startActivity(newActivity);
        }
    }
}

And this is the other List I want it to open up when I picked the first option

public class AnotherList extends ListFragment {

    ArrayList<String> storage = new ArrayList<String>(
            Arrays.asList("Test", "Test"));
    ArrayAdapter<String> adapter;

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

        adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, storage);

        setListAdapter(adapter);

    }
}

I get the following error message though

06-02 11:52:39.251: E/AndroidRuntime(1231): android.content.ActivityNotFoundException: Unable to find explicit activity class {.....}; have you declared this activity in your AndroidManifest.xml?

Do I have declare AnotherList in my manifest file? Weird if I have to since I did not do that with my first ListFragment.

UPDATE:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.list_fragment, new AnotherList());
ft.commit();

OLD main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/list_fragment"
        android:name="com.sanguosha.MyList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

NEW working main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/list_fragment"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" ></LinearLayout>

</LinearLayout>

Solution

    1. should be the Activity to start fragment not other fragment
    2. the snippet

      Intent newActivity = new Intent(v.getContext(), AnotherList.class);
      startActivity(newActivity);
      

    is wrong. You have to use startActivity to start an Activity not a Fragment. For fragment you have to use a FragmentTransaction