Android ListView运行逻辑正常但视图无内容显示问题求助
问题原因及修复方案
1. 适配器绑定错误
你在onCreate方法中实例化了自定义适配器ListaDinamica,但反而给ListView设置了空适配器,导致列表没有数据源渲染:
// 错误代码 ListaDinamica a = new ListaDinamica(this); lV.setAdapter(null);
修复为:
ListaDinamica a = new ListaDinamica(this); lV.setAdapter(a);
2. 列表项布局约束错误
所有列表项布局的第一个控件都设置了app:layout_constraintLeft_toRightOf="parent",该属性含义是将控件左边缘对齐父布局右边缘,直接把所有控件推到了屏幕可视区域外,自然看不到内容。同时列表项根布局高度用了match_parent,会导致单个列表项占满整个ListView高度,也会影响显示。
以emp_tecnologicas.xml为例,修复后的参考代码:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:padding="8dp"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/logo" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/nombreEmpresa" app:layout_constraintLeft_toRightOf="@id/logo" app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="12dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/localizacion" app:layout_constraintLeft_toRightOf="@id/nombreEmpresa" app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="12dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mail" app:layout_constraintLeft_toRightOf="@id/localizacion" app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="12dp"/> </androidx.constraintlayout.widget.ConstraintLayout>
emp_no_tecnologicas.xml按照同样逻辑修改即可:将第一个TextView的左约束改为对齐父布局左边缘,根布局高度改为wrap_content,给所有控件加顶部约束。
3. ListView宽度设置优化
你当前ListView宽度为wrap_content,在ConstraintLayout中建议改为match_parent撑满横向空间,避免显示不全:
<ListView android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/lista" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" />
内容的提问来源于stack exchange,提问作者Ozgewn
相关产品推荐
相关产品推荐

