[英]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.