You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView未完整显示数据库数据的问题排查请求

问题排查与修复:RecyclerView仅显示第一条任务数据

问题现状

开发学习用的简易任务管理器App,数据库已存储3条任务数据,但RecyclerView仅展示第一条,其余两条无法显示,怀疑ActivityResultLauncher存在问题。

数据库数据:

  • Testing 123 | Testing to see if work | 17/5/2024
  • Testing 456 | Testing 2nd time | 17/5/2024
  • Tsting again | Testing checking the database | 31/5/2024

核心问题定位

问题不在ActivityResultLauncher,而是列表项布局my_row.xml的根布局高度设置错误:根LinearLayout的android:layout_height="match_parent"会让每个列表项占据整个RecyclerView的可视高度,导致后续项被挤出屏幕,只能看到第一条。

修复方案

修改my_row.xml的根LinearLayout布局高度为wrap_content,让列表项高度自适应内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content" <!-- 将match_parent改为wrap_content -->
    android:orientation="vertical"
    android:padding="12dp"
    android:id="@+id/mainLayout">

    <!-- 下方CardView及内部布局保持不变 -->
    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="172dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="171dp">

            <TextView
                android:id="@+id/task_title_txt"
                android:layout_width="354dp"
                android:layout_height="38dp"
                android:layout_marginStart="13dp"
                android:layout_marginEnd="12dp"
                android:text="Title"
                android:textColor="@android:color/black"
                android:textSize="30sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                tools:ignore="HardcodedText,MissingConstraints"
                tools:layout_editor_absoluteY="7dp" />

            <TextView
                android:id="@+id/task_description_txt"
                android:layout_width="356dp"
                android:layout_height="67dp"
                android:layout_marginStart="13dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="12dp"
                android:text="Description"
                android:textSize="20sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/task_title_txt"
                tools:ignore="HardcodedText,MissingConstraints" />

            <TextView
                android:id="@+id/task_duedate_txt"
                android:layout_width="141dp"
                android:layout_height="29dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:text="Due Date"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/task_description_txt"
                tools:ignore="MissingConstraints" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

额外验证说明

  1. 数据加载逻辑正常:storeDataInArrays方法首次加载时会清空列表并遍历数据库所有数据,数组中确实包含3条任务数据;CustomAdapter的getItemCount()返回数组长度,RecyclerView的数据源数量是正确的。
  2. ActivityResultLauncher无问题:添加新任务后的回调逻辑正确,会更新数据并通知适配器刷新,修复布局后该功能可正常工作。

内容的提问来源于stack exchange,提问作者Phúc Đạt Trần

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 01:00:54