gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:46:31 24 4
gpt4 key购买 nike

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

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

最佳答案

对不起,我太笨了……我在初始化之前设置了 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);
}

关于java - Android Studio ListView 不会显示适配器中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com