How to pass list <object> between fragment

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

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 

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

List  result; ParseQuery 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 result; at SearchFragment, CardFragment and MapFragment.

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

回答1:

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 CREATOR = new             Parcelable.Creator() {                 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() ); 


回答2:

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 postDetailses; intent.putExtra("vdata", (Serializable) postDetailses); 

For Receiving at activity side

postDetailsList= (List) getIntent().getSerializableExtra("vdata"); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!