如何可能创建一个带有对象参数的片段?

huangapple go评论74阅读模式
英文:

How is it possible to create a fragment with an Object parameter?

问题

我想创建一个Fragment,并将一个复杂类型(MyClass)作为其参数。
这个指南实际上没有提到这一点。我能够找到一些使用Bundle的示例,但这些示例只包含诸如StringInteger之类的简单参数。

英文:

I would like to create a Fragment, and have a complex type (MyClass) as its parameter.
The guide does not really mention it. I was able to find some examples with Bundle, but those only contain simple parameters, like String and Integer.

答案1

得分: 2

Java版本:

public static MyFragment newInstance(int arg) {
    Bundle args = new Bundle();
    args.putInt("ARG", arg);

    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
}

Kotlin版本:

class MyFragment : Fragment() {
    companion object {
        fun newInstance(arg: Int): MyFragment {
            val args = Bundle()
            args.putInt("ARG", arg)
            val fragment = MyFragment()
            fragment.arguments = args
            return fragment
        }
    }
}

要在Bundle中传递自定义对象,您可以使您的类实现 ParcelableSerializable 接口。对于 Parcelable,您可以使用 putParcelable 方法将对象存储在 Bundle 中。

关于在 Android 中使用 ParcelableSerializable,请查看此讨论

您还会在 Android Studio 中找到一些插件,可以自动生成您的类的 Parcelable 实现代码。

另一种方法是使用 Gson 将对象转换为字符串,并将该字符串放入 Bundle 中。

有关使用 Gson,请查看这篇文章

英文:

You can create a static method named getInstance() or createInstance() or what ever you like for creating Fragment with some value

Java Version:

public static MyFragment newInstance(int arg) {

    Bundle args = new Bundle();
    args.putInt("ARG", arg);

    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
}

Kotlin Version:

class MyFragment : Fragment() {
    companion object {
       fun newInstance(arg: Int): MyFragment {
            val args = Bundle()
            args.putInt("ARG", arg)
            val fragment = MyFragment()
            fragment.arguments = args
            return fragment
        }
    }
}

> I was able to find some examples with Bundle, but those only contain
> simple parameters, like String and Integer.

To pass custom object in Bundle you can use make your class Parcelable or Serializable. For Parcelable you can use putParcelable method to store your object in Bundle.

For Parcelable and Serializable in Android check this thread.

You will find some plugin in Android Studio that will auto generate Parcelable implementation code for your class

Another approach is convert your object to String using Gson and put that String in Bundle.

For Gson check this post

答案2

得分: 0

你最好的选择可能是对复杂类进行序列化,然后在片段中进行反序列化。

所以类似这样:

bundle.putString("object_key", gson.toJson(yourClassInstance));

然后使用以下代码进行反序列化:

Type yourClassType = new TypeToken<YourClass>() {}.getType();
YourClass yourClassInstance = gson.fromJson(bundle.getString("object_key"), yourClassType);

或者,如果你的类不是泛型的:

gson.fromJson(bundle.getString("object_key"), YourClass.class);

更多信息请参阅Gson文档

英文:

Your best bet is probably serializing the complex class and deserializing it again in the fragment.

So something like

bundle.putString(&quot;object_key&quot;, gson.toJson(yourClassInstance));

And then deserialize it with

Type yourClassType = new TypeToken&lt;YourClass&gt;() {}.getType();
YourClass yourClassInstance = gson.fromJson(bundle.getString(&quot;object_key&quot;), yourClassType);

or, if your class isn't generic:

gson.fromJson(bundle.getString(&quot;object_key&quot;), YourClass.class);

For more information, see the Gson docs

huangapple
  • 本文由 发表于 2020年9月1日 04:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63677895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定