如何将ParcelableArrayList从Fragment传递到Fragment? [英] How to pass ParcelableArrayList from Fragment to Fragment?

查看:128
本文介绍了如何将ParcelableArrayList从Fragment传递到Fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将ParcelableArrayList从一个片段传递到另一个片段的方法,由于某种原因,当我收到ParcelableArrayList时,它是空的.我已经检查了很多有关此问题的帖子,但是我没有使它起作用.

I have been trying to find the way to pass a ParcelableArrayList from one Fragment to Another and for some reason when I receive the ParcelableArrayList it's empty. I have checked many posts about this problem but I don't make it work.

这是我的活动,将片段保存在FrameLayout中:

This is my Activity that holds the Fragments in a FrameLayout:

package com.blumonpay.restaurant.customer.presentation.activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.WindowManager;

import com.blumonpay.android.mifel.restaurante.R;
import com.blumonpay.restaurant.customer.presentation.fragments.ShowPayMethodsFragment;

public class PayMethodsActivity extends AppCompatActivity {
    private Fragment showFragment;
    private Fragment addFragment;
   


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_pay_methods);

        showFragment = ShowPayMethodsFragment.getInstance(this);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayout, showFragment)
                .commit();
        
    }

    public void switchFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.frameLayout, fragment)
                .commit();
    }

   
}

这就是我试图在第一个Fragment中传递ArrayList的方式:

This is how I'm trying to pass the ArrayList in my first Fragment:

        Bundle dataBundle = new Bundle();
        dataBundle.putParcelableArrayList("cardsList", (ArrayList<? extends Parcelable>) list);
        dataBundle.putInt("position",position);
        ShowDetailedPayMethodFragment showDetailPayMethod = ShowDetailedPayMethodFragment.getInstance(context);
        showDetailPayMethod.setArguments(dataBundle);
        ((PayMethodsActivity)context).switchFragment(showDetailPayMethod);

如您所见,我还传递了一个 int 值,该值我可以毫无问题地接收到.

As you can see I'm also passing an int value which I can receive with no problem at all.

然后,这是我正在接收数据的第二个片段( ParcelableArrayList int 值):

Then, this is the second Fragment where I'm receiving the data (ParcelableArrayList and the int value):

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_view_detailed_pay_method, container, false);

        if (this.getArguments()!=null) {
            cardsList = this.getArguments().getParcelableArrayList("cardsList");
            positionList = this.getArguments().getInt("position");
        }
        
        cardsBannerAdapter = new CustomCardsBannerAdapter(context, cardsList);
        cardsViewPager.setAdapter(cardsBannerAdapter);

        initSlider();
        return v;
    }

由于某种原因,当我到达这一点时,列表为空,我得到的唯一值是 this.getArguments().getInt("position"); (int值)

For some reason the List is empty when I get to this point, the only value I'm getting is this.getArguments().getInt("position"); (int value)

最后这是我的POJO类 Cards :

And finally this is my POJO class Cards:

package com.blumonpay.restaurant.customer.domain.model;

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

public class Cards implements Parcelable {

    private String cardNumber;
    private String cardType;
    private String cardBrand;
    private String cardExpirationDate;
    private String cardHolderName;

    public Cards(String cardNumber, String cardType, String cardBrand, String cardExpirationDate, String cardHolderName) {
        this.cardNumber = cardNumber;
        this.cardType = cardType;
        this.cardBrand = cardBrand;
        this.cardExpirationDate = cardExpirationDate;
        this.cardHolderName = cardHolderName;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCardType() {
        return cardType;
    }

    public void setCardType(String cardType) {
        this.cardType = cardType;
    }

    public String getCardBrand() {
        return cardBrand;
    }

    public void setCardBrand(String cardBrand) {
        this.cardBrand = cardBrand;
    }

    public String getCardExpirationDate() {
        return cardExpirationDate;
    }

    public void setCardExpirationDate(String cardExpirationDate) {
        this.cardExpirationDate = cardExpirationDate;
    }

    public String getCardHolderName() {
        return cardHolderName;
    }

    public void setCardHolderName(String cardHolderName) {
        this.cardHolderName = cardHolderName;
    }



    protected Cards(Parcel in) {
        cardNumber = in.readString();
        cardType = in.readString();
        cardBrand = in.readString();
        cardExpirationDate = in.readString();
        cardHolderName = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(cardNumber);
        dest.writeString(cardType);
        dest.writeString(cardBrand);
        dest.writeString(cardExpirationDate);
        dest.writeString(cardHolderName);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Cards> CREATOR = new Parcelable.Creator<Cards>() {
        @Override
        public Cards createFromParcel(Parcel in) {
            return new Cards(in);
        }

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

我想念什么的任何线索吗?

Any clue of what am I missing?

推荐答案

根据这篇文章,如何封送和拆封可包裹的邮件 Parcelable 对象不稳定,这很可能是您无法将其从一个活动传递到另一个活动的原因.我最近遇到了类似的问题,并使用相同的帖子解决了我的问题.基本上分步骤进行:1.您需要将第一个 Activity 中的 Parcelable 对象编组为:

According to this post, How to marshall and unmarshall Parcelable, Parcelable object is not stable which might well be the reason that you cannot pass it from one activity to another. I faced similar problem recently and used the same post to solve my problem. Basically in steps: 1. You need to marshall the Parcelable object in the first Activity as :

byte[] bytes = marshall(parcelableObj);

intentOpenSecondActivity.putExtra(SOME_KEY, bytes);

2.您需要在第二个 Activity 中将其解组为:

2. You need to unmarshall it in the second Activity as :

byte[] bytes = getIntent().getExtras().getByteArray(SOME_KEY);

parcelableObj = unmarshall(bytes, ParcelableObject.CREATOR);

这篇关于如何将ParcelableArrayList从Fragment传递到Fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆