首页 > 解决方案 > Android Studio中的列表视图不显示任何内容

问题描述

我正在使用 Java 在 Android Studio 中制作一个项目。

在此,我通过对话框从用户那里获取字符串输入,然后将它们存储在字符串数组中。然后使用数组适配器我试图在我的列表视图中获取字符串。

我使用了 2 个 .xml 文件。一个是activity_main.xml,另一个是subject_screen.xml

我想从 Activity_main.xml 布局启动我的应用程序,这就是为什么在我编写的 on Create 函数中setContentView(R.id.activity_main)。但是现在当我更改屏幕并转到包含 ListView 的 subject_screen.xml 时, ListView 不可见。

但是当我将行替换setContentView(R.id.activity_main)setContentView(R.id.subjects_screen)ListView 时是可见的。

如何解决这个问题呢?

subject_screen.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/TitleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:gravity="center"
    android:inputType="textPersonName"
    android:text="Subjects"
    android:textColor="@color/black"
    android:textSize="25dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/backButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:text="BACK"
    android:onClick="backToMain"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/removeButton"
    app:layout_constraintHorizontal_bias="0.517"
    app:layout_constraintStart_toEndOf="@+id/addButton" />

<Button
    android:id="@+id/removeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="50dp"
    android:text="REMOVE"
    android:onClick="removeSubject"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<Button
    android:id="@+id/addButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginBottom="50dp"
    android:text="ADD"
    android:onClick="addSubject"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<ListView
    android:layout_width="350dp"
    android:layout_height="400dp"
    app:layout_constraintBottom_toTopOf="@+id/backButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:id="@+id/subjectList"
    app:layout_constraintTop_toBottomOf="@+id/TitleText" />


</androidx.constraintlayout.widget.ConstraintLayout>

对于我正在使用的列表视图

    ArrayAdapter<String> subjectAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,subjectName);
    subjectList.setAdapter(subjectAdapter);

我检查了字符串数组 subjectName 是否正确。

标签: androidxmlandroid-studiolistview

解决方案


推荐阅读