Search code examples
javaandroidandroid-listfragment

Android: Add a listfragment to the transaction


I try to add the listfragment with fragmentTransation.add(...) but I can't do it, it is an error. I want to add a listfragment like a normal fragment, is it possible?

Here is my activity main code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentList fragmentList = FragmentList.createFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.relativeLayout,fragmentList); //WRONG
    fragmentTransaction.commit();
}

Here is my listfragment class code:

public class FragmentList extends ListFragment
{
public static FragmentList createFragment()
{
    FragmentList fragmentList = new FragmentList();
    return fragmentList;
}

@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    ListView listView = getListView();
    String[] stringa = {"A","B","C"};
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,stringa);
    listView.setAdapter(arrayAdapter);

}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    super.onCreateView(inflater,container,savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment, container, false);
    return rootView;
}
}

Solution

  • 1) You cannot use android.app.ListFragment with SupportFragmentManger you can use getFragmentManager() for that.

    Basically, check your import statements.

    Or you may extend android.support.v4.app.Fragment instead and define your own layout with a <ListView> (like you already are doing)

    Or you can use android.support.v4.app.ListFragment


    More importantly, though

    2) getListView() can't be called from onCreate because there is no view yet.

    You only need onCreateView

    public class FragmentList extends ListFragment {
    
        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            super.onCreateView(inflater,container,savedInstanceState);
            View rootView = inflater.inflate(R.layout.fragment, container, false);
            String[] stringa = {"A","B","C"};
    
            ListView listView = (ListView) rootView.findViewById(android.R.id.list);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,stringa);
            listView.setAdapter(arrayAdapter);
    
            return rootView;
        }
    }
    

    Plus,

    getFragmentManager() // Change accordingly
        .beginTransaction()
        .add(R.id.relativeLayout,new FragmentList())
        .commit();