Android ArrayList <MyObject> passes as possible

The code has now been modified to reflect the decision made.

Now this serves as a working example of how to pass a custom ArrayList to DialogFragment.

I am passing an ArrayList of custom objects to DialogFragment using Bundle on newInstance. Arraialist obtained correctly in newInstance. The call to putParcelable is excellent (no errors), but setting breakpoints in the batch code of the ArrayList object indicates that packet processing methods are not called when setting up or receiving data.

Am I creating the LocalityList class for ArrayList correctly and making it possible, or should there be a locality class itself?

DialogFragment

/** * Create a new instance of ValidateUserEnteredLocationLocalitySelectorFragment, providing "localityList" * as an argument. */ public static ValidateUserEnteredLocationLocalitySelectorFragment newInstance(LocalityList localityList) { ValidateUserEnteredLocationLocalitySelectorFragment fragmentInstance = new ValidateUserEnteredLocationLocalitySelectorFragment(); // Supply location input as an argument. Bundle bundle = new Bundle(); bundle.putParcelable(KEY_LOCALITY_LIST, localityList); fragmentInstance.setArguments(bundle); return fragmentInstance; } /** * Retrieve the locality list from the bundle */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocalityList = getArguments().getParcelable(KEY_LOCALITY_LIST); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.validate_user_entered_location, container, false); mLocalityListView = (ListView) view.findViewById(R.id.dialogLocalityListView); mAdapter = new SearchLocationLocalitiesListAdapter(getActivity(), mLocalityList); mLocalityListView.setAdapter(mAdapter); return view; } 

Class localitylist

 import java.util.ArrayList; import android.os.Parcel; import android.os.Parcelable; public class LocalityList extends ArrayList<Locality> implements Parcelable { private static final long serialVersionUID = 663585476779879096L; public LocalityList() { } @SuppressWarnings("unused") public LocalityList(Parcel in) { this(); readFromParcel(in); } private void readFromParcel(Parcel in) { this.clear(); // First we have to read the list size int size = in.readInt(); for (int i = 0; i < size; i++) { Locality r = new Locality(in.readString(), in.readDouble(), in.readDouble()); this.add(r); } } public int describeContents() { return 0; } public final Parcelable.Creator<LocalityList> CREATOR = new Parcelable.Creator<LocalityList>() { public LocalityList createFromParcel(Parcel in) { return new LocalityList(in); } public LocalityList[] newArray(int size) { return new LocalityList[size]; } }; public void writeToParcel(Parcel dest, int flags) { int size = this.size(); // We have to write the list size, we need him recreating the list dest.writeInt(size); for (int i = 0; i < size; i++) { Locality r = this.get(i); dest.writeString(r.getDescription()); dest.writeDouble(r.getLatitude()); dest.writeDouble(r.getLongitude()); } } } 

Locality class

 import android.os.Parcel; import android.os.Parcelable; public class Locality implements Parcelable { private String mDescription; private double mLatitude; private double mLongitude; public Locality(String description, double latitude, double longitude) { super(); this.mDescription = description; this.mLatitude = latitude; this.mLongitude = longitude; } public Locality(){ super(); } public String getDescription() { return mDescription; } public void setDescription(String description) { this.mDescription = description; } public double getLatitude() { return mLatitude; } public void setLatitude(double latitude) { this.mLatitude = latitude; } public double getLongitude() { return mLongitude; } public void setLongitude(double longitude) { this.mLongitude = longitude; } @SuppressWarnings("unused") public Locality(Parcel in) { this(); readFromParcel(in); } private void readFromParcel(Parcel in) { this.mDescription = in.readString(); this.mLatitude = in.readDouble(); this.mLongitude = in.readDouble(); } public int describeContents() { return 0; } public final Parcelable.Creator<Locality> CREATOR = new Parcelable.Creator<Locality>() { public Locality createFromParcel(Parcel in) { return new Locality(in); } public Locality[] newArray(int size) { return new Locality[size]; } }; @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(mDescription); dest.writeDouble(mLatitude); dest.writeDouble(mLongitude); } } 
+23
source share
3 answers

Yes, make the Locality class on its own, and remember to initialize

 ArrayList<Locality> mList= new ArrayList<Locality>(); 
+7
source

I know this question is quite old, but since I originally came here to look for answers, I wanted to share my experience.

Yes, you need to implement Parcelable for your Locality class, but that's it.

If your LocalityList is ONLY a wrapper for ArrayList, you do not need it.

Just use the putParcelableArrayList method.

 ArrayList<Locality> localities = new ArrayList<Locality>; ... Bundle bundle = new Bundle(); bundle.putParcelableArrayList(KEY_LOCALITY_LIST, localities); fragmentInstance.setArguments(bundle); return fragmentInstance; 

And remove it using ...

 localities = savedInstanceState.getParcelableArrayList(KEY_LOCALITY_LIST); 

So, if you don't need a custom ArrayList for some other reason, you can avoid doing any extra work and only implement Parcelable for your Locality class.

+9
source

The trick I usually use is to parse the list in Json using Gson (from Google). On the other hand, I just pass the string in Json back to the new list.

Never noticed any lag.

-1
source

Source: https://habr.com/ru/post/947622/


All Articles