简体   繁体   English

如何将带有其他对象数组的对象从一个片段传递到另一个片段?

[英]How to pass an object with an array of other objects from one fragment to another?

I have one object, let's call it Shop , and this object has an array of objects called Offers . 我有一个对象,我们称其为Shop ,并且该对象具有一组称为Offers的对象。 How should I use Bundle with my object to send it from a 1st Fragment to the one I open with a onClickListener ? 我应该如何将Bundle与我的对象一起使用,以将其从第一个片段发送到使用onClickListener打开的片段?

Beeing Shop , the object I need to parse something like: Beeing Shop ,我需要解析的对象类似于:

public class Shop {

    private String name;
    private String schedule;
    private String direction;
    private List<Offer> Offers = new ArrayList<Offer>();
}

and Offer Something like : Offer类似的东西:

public class Offer {

    private int packsAvailable;
    private String description;
}

and that function calls the second fragment: 该函数调用第二个片段:

public void onClick(View v) {


    Fragment shopping = new OffersDisplayFragment();

    //Instantiate fragment
    FragmentTransaction fragmentTransaction = 
    activity.getSupportFragmentManager()
        .beginTransaction();
    fragmentTransaction.replace(R.id.fragment, shopping);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

You can achieve it by adding a method to create an Instance of the next Fragment (I assume it is the OffersDisplayFragment). 您可以通过添加一种方法来创建下一个片段的实例来实现它(我假设它是OffersDisplayFragment)。

Add the following method to your fragment so we can create Fragment with bundle: 将以下方法添加到您的片段中,以便我们可以使用bundle创建Fragment:

public class OffersDisplayFragment extends Fragment {
    public static final String SHOP_EXTRA = "shopExtra";

    // Creates a new fragment given a Shop
    public static OffersDisplayFragment newInstance(Shop shop) {
        OffersDisplayFragment fragment = new OffersDisplayFragment();
        Bundle args = new Bundle();
        args.putSerializable(SHOP_EXTRA, shop);
        fragment.setArguments(args);
        return fragment;
    }
}

Then update or create onCreate method of your fragment to handle the bundle: 然后更新或创建片段的onCreate方法来处理捆绑包:

public class OffersDisplayFragment extends Fragment {
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Get back arguments
       Shop = (Shop) getArguments().getSerializable(SHOP_EXTRA);

       // Do something with the Shop here   
   }
}

in this point, Android Studio will complaining that your Shop model can't be serialized. 在这一点上,Android Studio将抱怨您的Shop模型无法序列化。 This is expected, because we haven't declare the Shop as serializable . 这是预料之中的,因为我们尚未将Shop声明为serializable So, change your Shop and Offer as serializable like this: 因此,将您的Shop and Offer更改为可序列化,如下所示:

public class Shop implements Serializable {
  ...
}

and

public class Offer implements Serializable {
  ...
}

Now, you can create the fragment with the bundle in your click listener like this: 现在,您可以在点击侦听器中使用捆绑创建片段,如下所示:

public void onClick(View v) {

    Fragment shoppingFragment = OffersDisplayFragment.newInstance(shop);

    ...
    // show the fragment with the fragment transaction.
}

After you have a working solution, try using Parcelable instead Serializable . 找到Parcelable解决方案后,请尝试使用Parcelable而不是Serializable

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM