简体   繁体   English

Android:如何将 Parcelable 对象传递给意图并使用包的 getParcelable 方法?

[英]Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

Why bundle has getParcelableArrayList , getParcelable methods;为什么 bundle 有getParcelableArrayListgetParcelable方法; but Intent has only putParcelableArrayListExtra method?但是Intent只有putParcelableArrayListExtra方法? Can I transmit only object<T> , not ArrayList of one element?我可以只传输object<T>而不是一个元素的ArrayList吗? Then, what is getParcelable for?那么, getParcelable有什么用呢?

Intent provides bunch of overloading putExtra() methods. Intent 提供了一堆重载putExtra()方法。

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity:假设您有一个类 Foo 正确实现了 Parcelable,将其放入 Activity 中的 Intent 中:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);

To get it from intent in another activity:从另一个活动的意图中获取它:

Foo foo = getIntent().getExtras().getParcelable("foo");

Hope this helps.希望这可以帮助。

Parcelable p[] =getIntent().getParcelableArrayExtra("parcel");

Sender Activity:发件人活动:

val intent = Intent(this, RestaurantDetails::class.java)
        intent.putExtra(Constants.RESTAURANT, restaurant)
        startActivity(intent)

Receiver Activity:接收器活动:

        val restaurant = intent.getParcelableExtra<Restaurant>(Constants.RESTAURANT)

It is important to remember that your models must implement the Parcelable interface, and the static CREATOR method.请务必记住,您的模型必须实现Parcelable接口和静态CREATOR方法。 This case is for the lists此案例适用于列表

 private static final String MODEL_LIST = "MODEL_LIST";
    public MainFragment() {}

    public static MainFragment newInstance(ArrayList<YourModel>   
models) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(MODEL_LIST,models);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            ArrayList<YourModel> models = getArguments().getParcelableArrayList(MODEL_LIST);
        }
    }

First create Parcelable using Given Technique then首先使用Given Technique创建 Parcelable 然后

public static CreditCardDetail newInstance(CreditCardItemBO creditCardItem) {
        CreditCardDetail fragment = new CreditCardDetail();
        Bundle args = new Bundle();
        args.putParcelable(CREDIT_KEY,creditCardItem);
        fragment.setArguments(args);
        return fragment;
    }

And getting it like并得到它喜欢

 if(getArguments() != null)
 {
    creditCardItem = getArguments().getParcelable(CREDIT_KEY);               
 }

where在哪里

public static final String CREDIT_KEY = "creditKey";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 null object 参考上调用虚拟方法 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' - Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference 尝试调用虚拟方法&#39;android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)&#39;空对象引用 - Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' null object reference android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' 在 null ZA8CFDE6331BD59EB666Z66331BD59EB66 - android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference 如何使用宗地将CountDownTimer对象传递给Intent? - How to pass object of CountDownTimer into intent using parcelable? Intent和Parcelable对象Android - Intent and Parcelable object Android 如何使用 android 中的 Parcelable 接口通过意图将包含可绘制参数的 object 传递给另一个活动 - how to pass an object containing with drawable parameter to another activity through intent using Parcelable Interface In android Android Intent开始活动-如何使用Bundle将参数传递给片段 - Android intent starts activity - how to use Bundle to pass parameters to a fragment 如何在Android中使用Intent传递可拆分变量值? - How to pass parcelable variable values using intent in android? 如何在意图中广播一个可分配的对象? - How to broadcast a parcelable object in an intent? android-无法将Intent中的包裹传递给另一个活动 - android - unable to pass parcelable in Intent to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM