ArrayList va Parcelable

Menga maxsus sinfning ArrayListini bir obyektdan boshqasiga "o'tkazish" kerak. Parcelable interfeysini amalga oshirishim kerakligini bilaman.

Bu mening maxsus sinfim.

public class cSportsList implements Parcelable {
    boolean checked;
    String name;

    cSportsList(boolean check, String name_) {
        checked = check;
        name = name_;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //Non posso passare un booleano e non posso fare il cast boolean -> int
        if (checked) {
            dest.writeInt(1);
        }
        else dest.writeInt(0);
        dest.writeString(name);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public cSportsList createFromParcel(Parcel in) {
            return new cSportsList(in);
        }

        public cSportsList[] newArray(int size) {
            return new cSportsList[size];
        }
    };

    private cSportsList(Parcel in) {
        if (in.readInt() == 1) {
            checked = true;
        }
        else {
            checked = false;
        }
        name = in.readString();


    }
}

Va bu, "

//This is sportsMap: ArrayList sportsMap = new ArrayList();
Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", (ArrayList<? extends Parcelable>)  sportsMap);  //I have tried with ArrayList too.
this.startActivity(intent);

Va bu, "shaxsga"

final Intent srcIntent = getIntent();
ArrayList sportsMap = srcIntent.getParcelableExtra("sportsMap");

Muammo shundaki, "SportMap" nusxasida null-ga teng. Agar "writeToParcel" va "cSportsList (parcelable in)" funktsiyalarida "to'xtash nuqtasi" ni o'rnatgan bo'lsam, kod ikkala funksiya uchun ham bajarilganligini ko'raman.

Mening xatoim qayerda?

Rahmat. M.

1
getParcelableArrayListExtra bilan getParcelableExtra ni almashtiring.
qo'shib qo'ydi muallif Rahul Sharma, manba

15 javoblar

O'qish paytida siz foydalanishingiz kerak

srcIntent.getParcelableArrayListExtra("sportsMap");

Niyat kodini quyida ishlatish

intent.putParcelableArrayListExtra("sportsMap", sportsMap);

uni niyatidan o'qish uchun foydalaning

ArrayList sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");

Read similar solution at Pass ArrayList<? implements Parcelable> to Activity

1
qo'shib qo'ydi

O'qish paytida siz foydalanishingiz kerak

srcIntent.getParcelableArrayListExtra("sportsMap");

Niyat kodini quyida ishlatish

intent.putParcelableArrayListExtra("sportsMap", sportsMap);

uni niyatidan o'qish uchun foydalaning

ArrayList sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");

Read similar solution at Pass ArrayList<? implements Parcelable> to Activity

1
qo'shib qo'ydi

O'qish paytida siz foydalanishingiz kerak

srcIntent.getParcelableArrayListExtra("sportsMap");

Niyat kodini quyida ishlatish

intent.putParcelableArrayListExtra("sportsMap", sportsMap);

uni niyatidan o'qish uchun foydalaning

ArrayList sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");

Read similar solution at Pass ArrayList<? implements Parcelable> to Activity

1
qo'shib qo'ydi

Arraylistni ushbu usuldan boshqa joyga o'tkazib yubormaslikka harakat qiling:

Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", sportsMap);
        startActivity(intent);

WhatSportActivity saytida,

final Intent srcIntent = getIntent();
ArrayList sportsMap = srcIntent.getParcelableExtra("sportsMap");

Agar siz hali ham muammoga duch kelsangiz, "Parceable" plaginini foydalanib, Parceable dasturini ishlatib ko'ring, chunki bu Parceable ro'yxatini ishlatishda xatolikni kamaytiradi.

0
qo'shib qo'ydi

Arraylistni ushbu usuldan boshqa joyga o'tkazib yubormaslikka harakat qiling:

Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", sportsMap);
        startActivity(intent);

WhatSportActivity saytida,

final Intent srcIntent = getIntent();
ArrayList sportsMap = srcIntent.getParcelableExtra("sportsMap");

Agar siz hali ham muammoga duch kelsangiz, "Parceable" plaginini foydalanib, Parceable dasturini ishlatib ko'ring, chunki bu Parceable ro'yxatini ishlatishda xatolikni kamaytiradi.

0
qo'shib qo'ydi

Arraylistni ushbu usuldan boshqa joyga o'tkazib yubormaslikka harakat qiling:

Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", sportsMap);
        startActivity(intent);

WhatSportActivity saytida,

final Intent srcIntent = getIntent();
ArrayList sportsMap = srcIntent.getParcelableExtra("sportsMap");

Agar siz hali ham muammoga duch kelsangiz, "Parceable" plaginini foydalanib, Parceable dasturini ishlatib ko'ring, chunki bu Parceable ro'yxatini ishlatishda xatolikni kamaytiradi.

0
qo'shib qo'ydi

Siz quyidagilarni ishlatishingiz kerak:

ArrayList sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");
0
qo'shib qo'ydi

Siz quyidagilarni ishlatishingiz kerak:

ArrayList sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");
0
qo'shib qo'ydi

Siz quyidagilarni ishlatishingiz kerak:

ArrayList sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");
0
qo'shib qo'ydi

Shu bilan bir qatorda, ketma-ketlikni ishlatishingiz mumkin.

public class cSportsList implements Serializable

qo'yish uchun:

intent.putExtra ("LIST", ro'yxat);

olish uchun:

(ArrayList<>)intent.getSerializableExtra("LIST");

0
qo'shib qo'ydi
O'qishdimki, Parcelable Serialize'dan yaxshiroqdir. Ammo, lekin: rahmat.
qo'shib qo'ydi muallif ZioBudda, manba

Shu bilan bir qatorda, ketma-ketlikni ishlatishingiz mumkin.

public class cSportsList implements Serializable

qo'yish uchun:

intent.putExtra ("LIST", ro'yxat);

olish uchun:

(ArrayList<>)intent.getSerializableExtra("LIST");

0
qo'shib qo'ydi
O'qishdimki, Parcelable Serialize'dan yaxshiroqdir. Ammo, lekin: rahmat.
qo'shib qo'ydi muallif ZioBudda, manba

Shu bilan bir qatorda, ketma-ketlikni ishlatishingiz mumkin.

public class cSportsList implements Serializable

qo'yish uchun:

intent.putExtra ("LIST", ro'yxat);

olish uchun:

(ArrayList<>)intent.getSerializableExtra("LIST");

0
qo'shib qo'ydi
O'qishdimki, Parcelable Serialize'dan yaxshiroqdir. Ammo, lekin: rahmat.
qo'shib qo'ydi muallif ZioBudda, manba

Quyidagi kodni ishlating

sportsMap = srcIntent.getParcelableArrayListExtra ("sportsMap");

0
qo'shib qo'ydi

Quyidagi kodni ishlating

sportsMap = srcIntent.getParcelableArrayListExtra ("sportsMap");

0
qo'shib qo'ydi

Quyidagi kodni ishlating

sportsMap = srcIntent.getParcelableArrayListExtra ("sportsMap");

0
qo'shib qo'ydi