现在修改了代码以反映已接受的解决方案。
现在这是一个如何将自定义ArrayList传递给DialogFragment的工作示例。
我使用newInstance上的Bundle将自定义对象的ArrayList传递给DialogFragment。 在newInstance中正确接收了arraylist。对putParcelable的调用执行正常(无错误),但在ArrayList对象的parcelable代码中放置断点表明在设置或获取数据时未调用parcel方法。
我是否正确为ArrayList创建一个LocalityList类并使其成为parcelable,或者Locality类本身是否可以分类?
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;
}
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());
}
}
}
地点类
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);
}
}
答案 0 :(得分:9)
我知道这个问题相当陈旧,但由于我最初来到这里寻找答案,我想分享我的经验。
是的,您需要为Locality
类实现Parcelable,但就是这样。
如果您的LocalityList
只是ArrayList的包装器,那么您不需要它。
只需使用putParcelableArrayList方法。
ArrayList<Locality> localities = new ArrayList<Locality>;
...
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(KEY_LOCALITY_LIST, localities);
fragmentInstance.setArguments(bundle);
return fragmentInstance;
使用...
检索它localities = savedInstanceState.getParcelableArrayList(KEY_LOCALITY_LIST);
因此,除非您出于某些其他原因需要自定义ArrayList,否则您可以避免执行任何额外的工作,并且只为您的Locality类实现Parcelable。
答案 1 :(得分:7)
是的,让Locality
类本身 Parcelable ,并且不要忘记初始化
ArrayList<Locality> mList= new ArrayList<Locality>();
答案 2 :(得分:-1)
我通常使用的技巧是使用Gson(来自谷歌)将列表解析为Json。另一方面,我只是将Json中的字符串分成一个新列表。
从未注意到任何滞后。