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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10107442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:30:26  来源:igfitidea点击:

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

androidandroid-intentbundleparcelable

提问by yital9

Why bundle has getParcelableArrayList, getParcelablemethods; but Intenthas only putParcelableArrayListExtramethod? Can I transmit only object<T>, not ArrayListof one element? Then, what is getParcelablefor?

为什么 bundle 有getParcelableArrayList,getParcelable方法;但 Intent只有putParcelableArrayListExtra方法?我可以只传输object<T>,而不是ArrayList一种元素吗?那么,到底是getParcelable为了什么?

回答by yorkw

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.

希望这可以帮助。

回答by MAC

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

回答by chry

It is important to remember that your models must implement the Parcelableinterface, and the static CREATORmethod. This case is for the lists

请务必记住,您的模型必须实现Parcelable接口和静态CREATOR方法。此案例适用于列表

 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);
        }
    }

回答by Jawad Zeb

First create Parcelable using Given Techniquethen

首先使用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";