簡體   English   中英

我正在嘗試在 Fragment 中創建一個微調器並使用 ArrayList 填充它,但應用程序不斷崩潰

[英]I'm trying to create a spinner in a Fragment and fill it using ArrayList but the app keeps crashing

這是我第一次在 Android 中使用 Fragments,我正在嘗試創建一個微調器並使用 ArrayList 填充它。 我的應用程序在主屏幕上有 3 個按鈕,第一個按鈕用這個片段替換了 Activity 中的 FrameLayout,但是一旦我按下它,應用程序就會崩潰。 我必須制作另外兩個片段,但我什至無法先制作這個片段。 這是一個項目。 請幫忙。 我將 XML 和 JAVA 包括在下面的片段中。

提前致謝。

<?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=".Book">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BOOK HOTELS"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.037" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="47dp"
        android:layout_marginTop="135dp"
        android:layout_marginEnd="316dp"
        android:layout_marginBottom="572dp"
        android:minHeight="48dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="SpeakableTextPresentCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA:

package com.example.hotelfragmentexercise;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.ArrayList;

public class Book extends Fragment implements AdapterView.OnItemSelectedListener {
   Spinner spinner;
   ArrayList<Hotels> hotelsList = new ArrayList<>();
   ArrayAdapter<Hotels> hotelsArrayAdapter;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_book, container, false);
       fillData();
       spinner = (Spinner) view.findViewById(R.id.spinner);

       hotelsArrayAdapter = new ArrayAdapter<Hotels>(getActivity(), android.R.layout.simple_spinner_item, hotelsList);

       hotelsArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinner.setAdapter(hotelsArrayAdapter);
       spinner.setOnItemSelectedListener(this);

       //return inflater.inflate(R.layout.fragment_book, container, false);
       return view;
   }

   public void fillData(){
       hotelsList.add(new Hotels("Town Inn Suites", "The Town Inn Suites is centrally located in downtown Toronto. Close to the shops of Bloor and only steps away from transportation at Bloor and Yonge.", 233.99));
       hotelsList.add(new Hotels("Times Square Suites Hotel", "Times Square Suites apartment-style hotel has been pleasing business travelers, couples and families for many years. Each of the 42 suites has a full kitchen,washer/dryer, fireplace, HD TV and blu ray player.", 294.78));
       hotelsList.add(new Hotels("Best Western The Westerly Hotel", "Courtenay's Only Full-Service Hotel Centrally Located in the Heart of the Comox Valley our location offers convenient access to all that Comox Valley has to offer. Our property features 66 guestrooms, inclusive of our 36 room Mountainside wing. ", 213.71));
       hotelsList.add(new Hotels("YMCA Hotel Vancouver", "YWCA Hotel Vancouver offers a warm welcome to all travellers and is a comfortable, safe and affordable place to stay at the heart of downtown's arts and entertainment scene. All rooms are private and there’s a choice to suit your needs and budget - from single rooms to quints.", 133.84));
       hotelsList.add(new Hotels("Grand Inn & Residence", "Settle in for a relaxing, just-like-home experience at one of Grande Prairie’s premier all-suite, extended stay hotels - Podollan Rez-idence. Designed and built to meet the needs of today’s discerning traveller, our all-suite property features beautifully appointed studio, one- and two-bedroom suites with fully equipped kitchens, and luxury living rooms with leather furnishings and wood floors.", 201.53));
   }

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {

   }
}
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
   // Inflate the layout for this fragment
   View view = inflater.inflate(R.layout.fragment_book, container, false);
   fillData();
   spinner = (Spinner) view.findViewById(R.id.spinner2);
    ArrayList<String> hotelsNames = new ArrayList<>();
    for (int i = 0; i < hotelsList.size(); i++) {
        hotelsNames.add(hotelsList.get(i).getName());
    }


   hotelsArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, hotelsNames);

   hotelsArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   spinner.setAdapter(hotelsArrayAdapter);
   spinner.setOnItemSelectedListener(this);

   //return inflater.inflate(R.layout.fragment_book, container, false);
   return view;
}

並且,將您的 ArrayAdapter 類型從 Hotels 更改為 String,因為它將返回酒店名稱而不是酒店對象。即

ArrayAdapter<Hotels> hotelsArrayAdapter /*change this line to*/ ->  ArrayAdapter<String> hotelsArrayAdapter /*this one*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM