Aayush Dua
Aayush Dua

Reputation: 13

List view inside fragment wont display the content

I am trying to implement a list view inside my fragment. I am using an array adapter to do it. The list view doesn't show up the data that i am adding inside the array.It also doesn't show any error.

Fragment Code

public class SymptomsFragment extends Fragment {

ArrayList<String> symptomsList = new ArrayList<>();
ListView listView;
ArrayAdapter<String> arrayAdapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View v =  inflater.inflate(R.layout.fragment_symptoms,container,false);
    listView = v.findViewById(R.id.symptomListView);
    symptomsList.add("Aches and pains");
    symptomsList.add("Sore throat");
    symptomsList.add("Diarrhoea");
    symptomsList.add("Conjunctivitis");
    symptomsList.add("Headache");
    symptomsList.add("Loss of taste or smell");
    symptomsList.add("Rashes on skin");
    arrayAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,symptomsList);


    listView.setAdapter(arrayAdapter);

    return inflater.inflate(R.layout.fragment_symptoms,container,false);
}
}

List View XML

<ListView
    android:id="@+id/symptomListView"
    android:layout_width="389dp"
    android:layout_height="367dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:background="@color/colorPrimary"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3" />

Upvotes: 1

Views: 31

Answers (2)

JML
JML

Reputation: 721

Your problem is in

return inflater.inflate(R.layout.fragment_symptoms,container,false);

You are inflating a layout, finding and configuring your ListView, and then throwing it away by inflating a new one and using that new one (without any configuration) as the return value for onCreateView

Just return the view you just inflated and configured

return v;

As a side note, please, don't use ListView, it was deprecated (a few years ago) and has lots of problems, instead, consider using a RecyclerView

Upvotes: 1

himel
himel

Reputation: 510

Try this

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View v =  inflater.inflate(R.layout.fragment_symptoms,container,false);
        listView = v.findViewById(R.id.symptomListView);
        symptomsList.add("Aches and pains");
        symptomsList.add("Sore throat");
        symptomsList.add("Diarrhoea");
        symptomsList.add("Conjunctivitis");
        symptomsList.add("Headache");
        symptomsList.add("Loss of taste or smell");
        symptomsList.add("Rashes on skin");
        arrayAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,symptomsList);


        listView.setAdapter(arrayAdapter);

        return v;
    }
    }

Upvotes: 1

Related Questions