Android. How to transfer the mass through Parcelable parcel
-
The class MyObject implements Parcelable was created to transfer the line from activism to fragment. The strings are great, but with the mass, there's a mistake. the brackets readArray(). I understand that the brackets need to write something or maybe not readArray() should be used.
All the code down.
public class MyObject implements Parcelable {
public String paramBuk;
public String[] paramImya;public MyObject(String paramBuk, String[] paramImya) {
this.paramBuk = paramBuk;
this.paramImya = paramImya;}
private MyObject(Parcel parcel) { // Создание объекта через Parcel
paramBuk = parcel.readString();
paramImya = parcel.readArray();}
public int describeContents() {
return 0;
}public void writeToParcel(Parcel parcel, int flags) { //Упаковывание объекта в Parcel
parcel.writeString(paramBuk);
parcel.writeArray(paramImya);}
public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() { // Статический метод с помощью которого создаем обьект
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}public MyObject[] newArray(int size) { return new MyObject[size]; }
};
}
public String paramBuk;
And with public String[] paramImya; something is wrong, underlines the square brackets readArray()
-
For reading
parcel
You can use the team:paramImya = parcel.createStringArray();
Maybe a quicker method that needs to know the size in advance.
N
massparamImya
:paramImya = new String[N]; parcel.readStringArray(paramImya);
And...
paramImya
counts will be stored.♪ http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/4.1.2_r1-robolectric-0/android/os/Parcel.java#Parcel.createStringArray%28%29 There's another method.
readStringArray()
which is somehow not listed https://developer.android.com/reference/android/os/Parcel.html#createStringArray() ♪ You may try to use it as follows:paramImya = parcel.readStringArray();
Well, to record the data.
parcel
May be teamed up:parcel.writeStringArray(paramImya);