Android Studio ListView将不会显示适配器中的项目

时间:2019-11-30 20:29:10

标签: java android android-listview

我刚刚开始学习Android,我尝试使用自定义适配器创建ListView。 我有一个片段,它集成了这样的ListView

<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" />

我创建了这样的自定义项目布局

<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>

这是我的自定义适配器代码


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;
    }
}

这就是我初始化的方式

  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()检索正确数量的项目,并且其中包含的项目不为null。那么为什么我的列表不显示它们...

1 个答案:

答案 0 :(得分:0)

对不起,我好笨... 我已经在初始化它之前设置了listView适配器... 这就是我固定的方式

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);
        }