ste9206
ste9206

Reputation: 1892

How to pass list <object> between fragment

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

    }
}

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

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?

Upvotes: 1

Views: 14618

Answers (2)

Sreehari
Sreehari

Reputation: 5655

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

Now comes how to implement Parceleable interface

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

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

Upvotes: 8

Mahesh Giri
Mahesh Giri

Reputation: 1840

Just do like this i also used it. Create one class of object which list you want to pass

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

}

In intent just put

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

For Receiving at activity side

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

Upvotes: -2

Related Questions