简体   繁体   English

"当我转换到下一页时,我的 ListView 没有出现"

[英]My ListView is not appearing when I transition to the next page

I am currently using the default layout on in my program.我目前在我的程序中使用默认布局。 The first page is good, it goes to the next page with no issues.第一页很好,它可以毫无问题地进入下一页。 The second page where I have the listview, it doesnt show我有列表视图的第二页,它没有显示

.java file .java 文件

package com.example.alimkutchhi_comp304_001_assign2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class SelectPhoneBrandActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_phone_brand);

        ListView listview = findViewById(R.id.lstvPhones);

        ArrayList<String> phones = new ArrayList<>();
        phones.add("iPhone");
        phones.add("Samsung");
        phones.add("Google Pixel");
        phones.add("Huawei");

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, phones);

    }
}

I followed a tutorial so I know this should be the correct format This is what I wrote in my .xml file我遵循了一个教程,所以我知道这应该是正确的格式这是我在 .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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SelectPhoneBrandActivity">

    <ListView
        android:id="@+id/lstvPhones"
        android:layout_width="379dp"
        android:layout_height="699dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Again, this should be correct, so maybe it has something to do with Android Studio?同样,这应该是正确的,所以也许它与 Android Studio 有关? Hopefully I can get an answer to this issue.希望我能得到这个问题的答案。

I was able to figure this one out thankfully.谢天谢地,我能够弄清楚这一点。 What I forgot to do was add listview.setAdapter(adapter);<\/code>我忘记做的是添加listview.setAdapter(adapter);<\/code> to it对它

The bottom line of the java file is now this java文件的底线现在是这个

 ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, phones);
        listview.setAdapter(adapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 文本未出现在我的ListView中 - Text not appearing in my ListView android:为什么没有元素出现在我的列表视图中? - android: why are no elements appearing in my listview? 使用扫描仪时,为什么我的JOptionPane没有出现? - Why is my JOptionPane not appearing when I use a Scanner? 如何在imacros中过渡到下一页? - How to make the transition to the next page in imacros? 在扫描仪收到输入之前,如何停止下一个println的显示? - How do I stop the next println from appearing before my scanner has received input? 没有出现ListView - ListView not Appearing 在我的android项目中,有两个类,并且在第一类上有一个按钮,当我单击按钮时,我需要查看下一页。 - In my android project there are two classes and there is a button on first class,i need to view next page when i click on the button. 调用自定义列表视图适配器时我的应用程序失败 - My app is failing when i call to my custom listview adapter 当我在我的 android 应用程序中使用滚动视图时,底部导航视图一直出现在我的键盘上方 - bottom navigation view keeps appearing above my keyboard when I used a scroll view in my android app 单击按钮移动到下一页时出错 - Error when i click the button for move to the next page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM