gpt4 book ai didi

java - 如何将 Parcelable ArrayList 从 Fragment 传递到 Fragment?

转载 作者:行者123 更新时间:2023-12-01 16:32:59 25 4
gpt4 key购买 nike

我一直在尝试找到将 ParcelableArrayList 从一个 fragment 传递到另一个 fragment 的方法,由于某种原因,当我收到 ParcelableArrayList 时它是空的。我已经检查了很多关于这个问题的帖子,但我没有让它发挥作用。

这是我的 Activity,它将 Fragment 保存在 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 的方式:

        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 值,我可以毫无问题地接收该值。

然后,这是我接收数据的第二个 fragment (ParcelableArrayListint 值):

 @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 value)

最后这是我的 POJO 类 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];
}
};
}

知道我错过了什么吗?

最佳答案

在 kotlin 中尝试了相同的操作,Cards.java 完好无损。该 list 获得通过。这是 fragment 。你能尝试比较一下吗?

以第一个 fragment 为例:

val item = Cards("gfhfhfhfh", "", "", "", "")
val list = ArrayList<Cards>()
list.add(item

将其传递为:(无显式转换)

bundle.putParcelableArrayList("list",list)

在下一个 fragment 中:

val l = arguments?.getParcelableArrayList<Cards>("list")

关于java - 如何将 Parcelable ArrayList 从 Fragment 传递到 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62013958/

25 4 0