Neil M.
Neil M.

Reputation: 689

How to add a ListFragment Using to layout

I am trying to add a ListFragment into a Layout in java. However I get a error mesage saying add() in FragmentTransaction can not be applied. I was able to fix this by changing Fragment manger from being equal to getSupportFragmentManger to getFragmentManger. This is fine but what if I was developing for early versions of android that need the support libary. What can I do to fix this problem thanks in advance.

    package com.listfragment.example.listfragmentexample;

    import android.os.Bundle;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.ActionBarActivity;   
     public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            FragmentManager manager;
            manager = getSupportFragmentManager();
            MyListFragment frag1 = new MyListFragment();

            FragmentTransaction fragmentTransaction = manager.beginTransaction();
            fragmentTransaction.add(R.id.main,  frag1, "Fragment1");
            fragmentTransaction.commit();
        }
    }

    package com.listfragment.example.listfragmentexample;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

/**
 * Created by Neil on 12/27/2014.
 */
public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener {

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter =ArrayAdapter.createFromResource(getActivity(), R.array.heros, android.R.layout.simple_list_item_1 );
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

   /* @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(),  "Item" + position, Toast.LENGTH_SHORT ).show();
    }*/

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "Item" + position, Toast.LENGTH_SHORT).show();
    }
}

The error Message Screenshot https://i.sstatic.net/GE8s9.png

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@android:id/empty" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list" />


</LinearLayout>

activity_main below

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/main"
    >

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</RelativeLayout>

Upvotes: 0

Views: 806

Answers (1)

Rohit5k2
Rohit5k2

Reputation: 18112

Import

import android.support.v4.app.ListFragment

in place of

import android.app.ListFragment;

in class MyListFragment

Upvotes: 1

Related Questions