如何在片段之间传递列表<object>

时间:2016-05-13 10:55:58

标签: android android-fragments parse-platform

我已经使用viewPager和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()方法):

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

现在我想在List <ParseObject> result;SearchFragmentCardFragment传递MapFragment

可以使用Bundle吗?或者让我使用其他方法?

2 个答案:

答案 0 :(得分:6)

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

现在介绍如何实现Parceleable接口

创建要传递工具Parcelable接口

的对象类
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类

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

通过此

将其发送给下一个意图
Intent intent=new Intent(this, DetailView.class);
intent.putExtra("Data", contactPojo);

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

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

答案 1 :(得分:-2)

就这样,我也用过它。 创建一个您要传递的列表对象

public class PostDetails implements Serializable {
private String postUrl;
private String postType;
public String getPostUrl() {
return postUrl;
}

/**
* 
* @param postUrl
* The post_url
*/
public void setPostUrl(String postUrl) {
this.postUrl = postUrl;
}

/**
* 
* @return
* The postType
*/
public String getPostType() {
return postType;
}

/**
* 
* @param postType
* The post_type
*/
public void setPostType(String postType) {
this.postType = postType;
}

}

意图只是把

List<PostDetails> postDetailses;
intent.putExtra("vdata", (Serializable) postDetailses);

对于活动方面的接收

postDetailsList= (List<PostDetails>) getIntent().getSerializableExtra("vdata");