Search code examples
javaandroidandroid-listview

Android Studio ListView won't show items from adapter


i just started learning Android and i try to create an ListView with a custom adapter. I have a Fragment that integrates the ListView like this

<androidx.constraintlayout.widget.ConstraintLayout 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">


<ListView
    android:id="@+id/houseList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.6" />

I created a custom item layout like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <TextView
        android:id="@+id/houseDescription"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/houseLocation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_above="@id/houseDescription"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:textSize="16sp" />

</RelativeLayout>

Here is my Custom Adapter Code


public class HouseListAdapter extends ArrayAdapter<House> {

    public HouseListAdapter(Context context, List<House> users) {
        super(context, 0, users);
    }

    @NotNull
    @Override
    public View getView(int position, View convertView, @NotNull ViewGroup parent) {
        House house = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.house_list_view_layout, parent, false);
        }
        TextView houseLocation = convertView.findViewById(R.id.houseLocation);
        TextView houseDescription = convertView.findViewById(R.id.houseDescription);


        houseLocation.setText(house.getHouseAddress());
        houseDescription.setText(String.format("%s / %s", house.getOwnerName(), house.getOwnerPhone()));
        return convertView;
    }
}

and this is how I initialise it

  repository = HouseRepository.getInstance();
        adapter = new HouseListAdapter(root.getContext(), new LinkedList<House>());
        houseListView.setAdapter(adapter);
        List<House> houses = repository.searchHouses(10,0,"Id", OrderType.ASC).getValue();
        for (House house : houses) {
            adapter.add(house);
            adapter.notifyDataSetChanged();
        }
        return root;

adapter.getCount() retrieves the right number of items, and the items it contains are not null. So why my list does not show them...


Solution

  • I'm sorry i'm so dumb... i've seted the listView adapter before initialising it... This is how i fixed it

    ListView houseListView = root.findViewById(R.id.houseList);
            repository = HouseRepository.getInstance();
            houses = repository.searchHouses(10, 0, "Id", OrderType.ASC).getValue();
            adapter = new HouseListAdapter(root.getContext(), houses);
            adapter.setNotifyOnChange(true);
            houseListView.setAdapter(adapter);
            for (House house : houses) {
                adapter.add(house);
            }