Yuliy Daniel
Yuliy Daniel

Reputation: 41

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). I have trouble adding the arraylists to the Parcel.

Currently I use Parcel.readArrayList(String.class.getClassLoader()); to get an ArrayList and 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>'

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;
    }
}

Upvotes: 1

Views: 1551

Answers (1)

Sibtain Raza
Sibtain Raza

Reputation: 213

Check this out

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

Upvotes: 1

Related Questions