如何通过列表< object>片段之间 [英] How to pass list <object> between fragment

查看:68
本文介绍了如何通过列表< object>片段之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用viewPager和TabLayout创建了布局:

I've created my layout with viewPager and TabLayout:

public class HomeFragment extends Fragment
{


public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View inflatedView = inflater.inflate(R.layout.fragment_home, container,false);


    viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager);
    int [] drawables = {R.drawable.home,R.drawable.street,R.drawable.map};

    Bundle bundle = new Bundle();

    final SearchFragment searchFragment = new SearchFragment();
    final CardFragment cardFragment = new CardFragment();
    final MapFragment mapFragment = new MapFragment();

    viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager())
    {
        public Fragment getItem(int position)
        {
            if(position == 0)
             return searchFragment;
            else if(position ==1)
             return cardFragment;
            else
             return mapFragment;
        }

        @Override
        public int getCount()
        {
            return int_items;
        }
    });

    tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    for (int i = 0; i < tabLayout.getTabCount(); i++)
    {
        tabLayout.getTabAt(i).setIcon(drawables[i]);
    }

    return inflatedView;

}





    @Override
    public void onAttach(Context context) {

        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }
}

现在我需要在Asynctask中执行此代码(我仅发布doInBackground()方法):

now I need to do this code in Asynctask (I post only doInBackground() method):

List <ParseObject> result;
ParseQuery<ParseObject> query;

 protected Void doInBackground(final Void... params)
 {
   try
   {
      query = ParseQuery.getQuery("Trains");
      result = query.find();

   }
   catch (com.parse.ParseException e)
   {
    e.printStackTrace();
   }
}

所以现在我想在SearchFragmentCardFragmentMapFragment处通过List <ParseObject> result;.

so now I want to pass List <ParseObject> result; at SearchFragment, CardFragment and MapFragment.

可以使用捆绑软件吗?还是我要使用其他方法?

It's possible to use a Bundle? Or have I to use other method?

推荐答案

由于大多数开发人员都对Serialization和Parcelable的性能参数感到困惑,因此我在下面解释一下

As most of the developers are confused with performance parameters of Serialization and Parcelable, Im putting the explanation below

  • Comparison of Parceleable and Serialization with performance results Comparison b/w Parcelable and Serialization
  • Inorder to simplify parcelable implementation , there are plugin's available for IDE's Refer this Link for naXa's Answer
  • Plugin for Parcelable generator Android Studio Plugin for Android Parcelable boilerplate code generation

现在介绍如何实现Parceleable接口

Now comes how to implement Parceleable interface

创建要传递工具Parcelable接口的对象类

Create object class which you want to pass implement Parcelable interface

public class ContactPojo implements Parcelable{
       private String name;
       private String job_title;
       public void setName(String name) {
        this.name = name;
       }

       public void setJob_title(String job_title) {
        this.job_title = job_title;
       }
    public String getName() {
        return name;
    }

    public String getJob_title() {
        return job_title;
    }
    private ContactPojo(Parcel parcel){
        name=parcel.readString();
        job_title=parcel.readString();
    }
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeString(name);
        parcel.writeString(job_title);
    }
public static final Parcelable.Creator<ContactPojo> CREATOR = new
            Parcelable.Creator<ContactPojo>() {
                public ContactPojo createFromParcel(Parcel in) {
                    return new ContactPojo(in);
                }

                public ContactPojo[] newArray(int size) {
                    return new ContactPojo[size];
    }};
}

现在通过执行以下操作填充pojo类

Now populate the pojo class by the doing the following

ContactPojo contactPojo= new ContactPojo();
contactPojo.setName("name");
contactPojo.setJob_title("name");

以此发送它到下一个意图

send it to next intent by this

Intent intent=new Intent(this, DetailView.class);
intent.putExtra("Data", contactPojo);

通过后续步骤检索下一个意图中的数据

Retrieval of data in next intent by next steps

ContactPojo contactPojo=new ContactPojo();
contactPojo=getIntent().getParcelableExtra("Data");
Log.i(AppConstants.APPUILOG, "Name: " + contactPojo.getName() );

这篇关于如何通过列表&lt; object&gt;片段之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆