Android: ListView 仅显示第一项。 (请参考提供的图片)

Android: ListView shows only first item. (Please refer image provided)

我正在尝试将数据从 firebase 检索到 ArrayList 并在 ListView 上显示信息。我使用了自定义视图,但由于某种原因,只显示了第一条记录。我通过使用 Toast 显示详细信息进行了仔细检查,其他记录显示在那里,但它们没有显示在 ListView.

Android ListView 只显示第一项

CustomAdapter.java

class CustomAdapter extends ArrayAdapter<String> {
    private int layout;
    public CustomAdapter(@NonNull Context context, int resource, @NonNull ArrayList<String> objects) {
        super(context, resource, objects);
        layout = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final String details = getItem(position);
        ViewHolder viewHolder;
        ViewHolder mainViewHolder = null;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.dealInfo = convertView.findViewById(R.id.dealinfo);
            viewHolder.accept = convertView.findViewById(R.id.accept);
            viewHolder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(), details, Toast.LENGTH_SHORT).show();
                }
            });

            convertView.setTag(viewHolder);
        }else{
            mainViewHolder = (ViewHolder) convertView.getTag();
            mainViewHolder.dealInfo.setText(details);
        }

        return convertView;
    }
}

class ViewHolder{
    TextView dealInfo;
    Button accept;
}

我的 ListView 项目布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        ...
        ..
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/dealinfo"
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:fontFamily="monospace"
            android:textColor="#ffffff"
            android:textSize="15sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/accept"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/dealinfo"
            android:background="#000000"
            android:text="Accept"
            android:textColor="#ffffff"
            android:textStyle="bold" />


    </RelativeLayout>

我的列表视图xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:
    ...
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ManagerActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentTop="true"
        android:text="MANAGER"
        android:textAlignment="center"
        android:gravity="center"
        ..../>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_below="@+id/title"
        android:layout_alignParentBottom="true"
        android:background="#1fa781"
        android:divider="#ffffff"
        android:dividerHeight="6dp">

    </ListView>


</RelativeLayout>

编辑: 我删除了 getView() 中的 else 语句,它现在可以正常工作了。为什么这样? 这就是我所做的:

@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final String details = getItem(position);
        ViewHolder viewHolder;
        ViewHolder mainViewHolder = null;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.dealInfo = convertView.findViewById(R.id.dealinfo);
            viewHolder.accept = convertView.findViewById(R.id.accept);
            viewHolder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(), details, Toast.LENGTH_SHORT).show();
                }
            });

            convertView.setTag(viewHolder);
        }
        {
            mainViewHolder = (ViewHolder) convertView.getTag();
            mainViewHolder.dealInfo.setText(details);
        }

        return convertView;
    }

试试这个,然后告诉我发生了什么事

final String details = getItem(position);
    ViewHolder viewHolder;
    ViewHolder mainViewHolder = null;
    if(convertView == null){
        LayoutInflater inflater = LayoutInflater.from(getContext());
        //...
        //others codes
        //...
        ViewHolder.dealInfo.setText(details);//ADD THIS LINE
        convertView.setTag(viewHolder);
    }