简体   繁体   English

你如何将 ArrayList 的字符串放入 Android Java 的 Parcelable 中?

[英]How do you put/read an ArrayList of Strings into Parcelable in Android Java?

I use Android Studio and am trying to create a parcelable class containing four fields: a String representing the name of the Object, an arraylist of strings representing the names of the people, an arraylist of strings representing the objects, an arraylist of strings representing the places (It is actually a cluedo game configuration, if that is of any interest to the reader).我使用 Android Studio 并尝试创建一个可打包的 class,其中包含四个字段:表示 Object 名称的字符串,表示人名的字符串 arraylist,表示对象的字符串 arraylist,表示对象的字符串 88178588976地方(这实际上是一个 cluedo 游戏配置,如果读者对此感兴趣的话)。 I have trouble adding the arraylists to the Parcel.我在将 arraylists 添加到 Parcel 时遇到了问题。

Currently I use Parcel.readArrayList(String.class.getClassLoader());目前我使用Parcel.readArrayList(String.class.getClassLoader()); to get an ArrayList and Parcel.writeStringList(ArrayList<String>);得到一个 ArrayList 和Parcel.writeStringList(ArrayList<String>); to put an ArrayList. However, Android Studio gives me a warning Unchecked assignment: 'java.util.ArrayList to java.util.ArrayList<java.lang.String>'放一个 ArrayList。然而,Android Studio 给了我一个警告Unchecked assignment: 'java.util.ArrayList to java.util.ArrayList<java.lang.String>'

Please tell me what I am doing wrong and how to do it correctly.请告诉我我做错了什么以及如何正确地做。

package com.example.cluedoservice;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Arrays;

public class configInfo implements Parcelable {
    protected String name = "default";
    protected ArrayList<String> people, things, places;

    protected configInfo(String name, ArrayList<String> people,
                         ArrayList<String> things, ArrayList<String> places) {
        this.name = name;
        this.people = people;
        this.things = things;
        this.places = places;
    }

    protected configInfo(Parcel in) {
        name = in.readString();
        people = in.readArrayList(String.class.getClassLoader());
        things = in.readArrayList(String.class.getClassLoader());
        places = in.readArrayList(String.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel out, int i) {
        out.writeString(name);
        out.writeStringList(people);
        out.writeStringList(things);
        out.writeStringList(places);
    }

    public static final Creator<configInfo> CREATOR = new Creator<configInfo>() {
        @Override
        public configInfo createFromParcel(Parcel in) {
            return new configInfo(in);
        }

        @Override
        public configInfo[] newArray(int size) {
            return new configInfo[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }
}

Check this out看一下这个

people = in.createStringArrayList();
things = in.createStringArrayList();
places = in.createStringArrayList();

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

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