Search code examples
javaandroidlistviewandroid-fragmentsandroid-arrayadapter

ListView not showing up in fragement


I'm trying to use an array adapter for a ListView in a fragment, but for some reason whenever I run it doesn't show up at all. I'm not sure where I'm going wrong. Really new to this so there's probably a lot of room for errors.

here's the code for the fragment:

public class FirstFragment extends Fragment {

    ArrayList<String> arrayList=new ArrayList<>();
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        View firstLayOut = inflater.inflate(R.layout.fragment_first, container, false);
        ListView listView=firstLayOut.findViewById(R.id.listview);

        arrayList.add("apple");
        arrayList.add("banana");
        arrayList.add("cat");
        arrayList.add("dog");
        arrayList.add("egg");
        arrayList.add("f");
        arrayList.add("g");
        arrayList.add("h");
        arrayList.add("k");
        arrayList.add("l");
        arrayList.add("m");
        ArrayAdapter adapter1=new ArrayAdapter(firstLayOut.getContext(),android.R.layout.simple_list_item_1,arrayList);

        listView.setAdapter(adapter1);
        return firstLayOut;
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}

Here's the xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/BackgroundLight"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

    </ListView>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/BackgroundGray"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme">


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="To frag2"
            app:backgroundTint="@color/buttonBlueL" />
    </androidx.appcompat.widget.Toolbar>


</RelativeLayout>

and the code for the main activity:

public class MainActivity extends AppCompatActivity {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

and the xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/BackgroundLight"
    tools:context=".MainActivity">


    <include
        layout="@layout/fragment_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

Please let me know what I'm doing wrong.


Solution

  • Actually you are just including fragment_first layout to your main xml, instead of including fragment_first in main xml,add a container to that position and then replace this container with your fragment, your main xml will look like as

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/BackgroundLight"
        tools:context=".MainActivity">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_container" />
      </RelativeLayout>
    

    After that replace this main_container to your fragment as follows

    public class MainActivity extends AppCompatActivity {
    
        Fragment FirstFragment;
        FragmentManager fragmentManager;
        FragmentTransaction transaction;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            // getting fragment manager and begin transaction and finally replace your main_container to fragment
    
            getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new FirstFragment(),"First Fragment").commit();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    }