简体   繁体   中英

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

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 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? 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> to it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM