Home > database >  How to display ListItem in Fragment
How to display ListItem in Fragment

Time:09-23

I want to create Fragment with ListView, so, I have a ListView with listitem in my Fragment activity, but my listitem is not displayed when i open my fragment, i need help with it, how display items in fragment? When i make this ListView in Activity, all is working well, how fix this? My code here:

FollowFragment.java

public class FollowFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().setTitle("Подписки");
        return inflater.inflate(R.layout.fragment_follow,container,false);
    }
}

follow_fragment.xml

<?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"
    android:padding="10dp">

    <ListView
        android:id="@ id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/item_follow"/>

</FrameLayout>

item_follow.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/favourite_match_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp">


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/team_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/roboto_medium"
            android:text="FC Chelsea"
            android:textColor="@color/black"
            tools:ignore="MissingConstraints" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/team_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="@font/roboto_medium"
            android:text="Manchester City"
            app:layout_constraintTop_toBottomOf="@ id/team_1"
            android:textColor="@color/black"
            tools:ignore="MissingConstraints" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/match_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/roboto"
            android:text="25 сентября 2021 года"
            android:textColor="@color/colorGrey"
            android:textSize="12sp"
            app:layout_constraintTop_toBottomOf="@id/team_2"
            tools:ignore="MissingConstraints" />


        <ImageButton
            android:layout_width="48dp"
            android:layout_height="50dp"
            android:layout_marginEnd="24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/ic_love"
            android:background="#00000000"
            app:layout_constraintLeft_toRightOf="@id/team_2"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

CodePudding user response:

well that's because you didn't assign an adapter with the correct data to it yet.

  • your adapter should extend ArrayAdapter and override getView in which you should set your view with the correct content based on your data.

what an adapter might look like :

public class MyAdapter extends ArrayAdapter<YourDataCustomClassHere> {
    ArrayList<YourDataClassHere> data;
    Context context ;
    public MyAdapter(@NonNull Context context, int resource, @NonNull ArrayList<YourDataCustomClassHere> objects) {
        super(context, resource, objects);
        this.data = objects;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_follow, parent, false);
        }
        //example on setting one of your fields 
        ((AppCompatTextView) convertView.findViewById(android.R.id.team_1))
                .setText(data.get(position).getText());
        //set Other Fields here
        return convertView;
    }
}

where your data class should be a custom class that holds the correct information for each item in the list and getText in data.get(position).getText() is an arbitary method I assumed that will be in your custom class to get some sort of text but you're free to replace it with what you want.

  • then in your onCreateView you should set the adapter to your ListView like that :
public class FollowFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().setTitle("Подписки");
        View rootView= inflater.inflate(R.layout.fragment_follow,container,false);
        ListView list = rootView.findViewById(R.id.listview);
        MyAdapter myadapter = new MyAdapter(getContext(),R.layout.item_follow,YourDataArrayListHere);
        list.setAdapter(myadapter);
        return rootView; 
    }
}

I trust that you'll find your way and replace YourDataArrayListHere with the correct ArrayList of your data custom class.

anyway I hope you reconsider your choice of ListView as it consume much Ram (Memory) during runtime and use a RecyclerView instead

  • Related