I want to add a ListView to a Fragment. I created a FrameLayout in my activity_main.xml
and then created 3 fragments (Home, Tags, Settings). What I did was create a bottom navigation view and created 3 frame layouts for the mentioned fragments.
I searched the whole web on how to add a ListView to a Fragment, but whatever code they say is deprecated. I just want to add a ListView to the settings fragment. The code is below:
package com.bhiruva.dashboard.fragments; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import com.bhiruva.R; public class FragmentSettings extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; public FragmentSettings() { // Required empty public constructor } public static FragmentSettings newInstance(String param1, String param2) { FragmentSettings fragment = new FragmentSettings(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_settings, container, false); } }
This is my XML code:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context="com.bhiruva.dashboard.fragments.FragmentSettings"> </FrameLayout>
Can anyone please help me with the updated code for doing this? Thanks in Advance!
Advertisement
Answer
I’m going to assume you got the fragment to show on screen successfully and you just want to add a simple ListView
. Here’s how you do it:
1- Add the ListView
to your fragmnet xml with an id:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
2- Get a reference to it in onCreateView
of your fragment:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View contentView = inflater.inflate(R.layout.fragment_settings, container, false); ListView listView = contentView.findViewById(R.id.listview); return contentView; }
3- Create a Custom Adapter for your ListView
that extends BaseAdapter
:
class CustomAdapter extends BaseAdapter { List<String> items; public CustomAdapter(List<String> items) { super(); this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int i) { return items.get(i); } @Override public long getItemId(int i) { return items.get(i).hashCode(); } @Override public View getView(int i, View view, ViewGroup viewGroup) { TextView textView = new TextView(getContext()); textView.setText(items.get(i)); return textView; } }
I created a simple one here that only makes hosts a list of strings and displays each with a TextView, but you can replace it with whatever you want.
4- Set the adapter of the ListView
to your custom adapter:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View contentView = inflater.inflate(R.layout.fragment_settings, container, false); ListView listView = contentView.findViewById(R.id.listview); // sample data List<String> list = new ArrayList<>(); for(int i=0;i<100;i++) list.add("Item "+i); CustomAdapter listAdapter = new CustomAdapter(list); listView.setAdapter(listAdapter); return contentView; }
Done. You get a ListView in the fragment with all the items. As others have pointed out, ListView is not preferred when you have a large list of items, it’s bettter to use RecyclerView in that case.