android - 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 是否正确。
解决方案
推荐阅读
- java - 为 UserCredentials 和 UserPermissions 设计带有嵌入式类的 Spring-Security UserDetails 类
- html - 你们能帮帮我吗?为什么引导卡留在移动视图中
- pycharm - PyCharm - 如何更改鼠标颜色
- java - 在 opentok Android 中截取订阅者的屏幕截图
- git - 从 git diff 日志中删除索引
- distcc - 使用 distcc 运行构建时如何检查列出的所有服务器是否实际正在使用
- vba - 如何在 Powerdesigner 中访问扩展集合的子记录?
- azure-pipelines - azure data factory v2 从数据湖中摄取具有不同文件名和结构的文件
- css - 难以在应用模板中自定义 body bgcolor;尝试将应用程序名称移动到 INSTALLED APPS 的 django 管理应用程序上,覆盖引导程序
- flutter - 在服务器上上传多张图片无法正常工作