安卓的ArrayList&LT

安卓的ArrayList&LT

本文介绍了安卓的ArrayList<为MyObject>通过为parcelable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code现在修改,以反映接受的解决方案。现在,这作为如何通过自定义的ArrayList成DialogFragment工作的例子。

我传递的自定义对象的ArrayList使用一个Bundle对newInstance有一个DialogFragment。该ArrayList是的newInstance正确接收。以putParcelable调用执行罚款(没有错误),但把断点在parcelable code在ArrayList对象表明,包裹方法不被设置或获取数据时调用。

我是正确创建LocalityList类的ArrayList和使这一parcelable,还是应该局部性类本身是parcelable?

DialogFragment

  / **
 *创建ValidateUserEnteredLocationLocalitySelectorFragment的新实例,提供localityList
 *作为参数。
 * /
公共静态ValidateUserEnteredLocationLocalitySelectorFragment的newInstance(LocalityList localityList){

    ValidateUserEnteredLocationLocalitySelectorFragment fragmentInstance =新ValidateUserEnteredLocationLocalitySelectorFragment();

    //供应区位输入法作为参数。
    束束=新包();
    bundle.putParcelable(KEY_LOCALITY_LIST,localityList);
    fragmentInstance.setArguments(包);

    返回fragmentInstance;
}


/ **
 *检索从包当地列表
 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    mLocalityList = getArguments()getParcelable(KEY_LOCALITY_LIST)。
}


 @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

    查看查看= inflater.inflate(R.layout.validate_user_entered_location,集装箱,假);

    mLocalityListView =(ListView控件)view.findViewById(R.id.dialogLocalityListView);
    mAdapter =新SearchLocationLocalitiesListAdapter(getActivity(),mLocalityList);
    mLocalityListView.setAdapter(mAdapter);

    返回查看;
}
 

LocalityList类

 进口的java.util.ArrayList;

进口android.os.Parcel;
进口android.os.Parcelable;

公共类LocalityList扩展的ArrayList<局部性>实现Parcelable {

    私有静态最后长的serialVersionUID = 663585476779879096L;

    公共LocalityList(){
    }

    @燮pressWarnings(未使用)
    公共LocalityList(包裹中){
        本();
        readFromParcel(在);
    }

    私人无效readFromParcel(包裹中){
        this.clear();

        //首先我们要读列表的大小
        INT大小= in.readInt();

        的for(int i = 0; I<大小;我++){
            局部性R =新的地点(in.readString(),in.readDouble(),in.readDouble());
            this.add(r)的;
        }
    }

    公众诠释describeContents(){
        返回0;
    }

    公众最终Parcelable.Creator< LocalityList> CREATOR =新Parcelable.Creator< LocalityList>(){
        公共LocalityList createFromParcel(包裹中){
            返回新LocalityList(中);
        }

        公共LocalityList [] newArray(INT尺寸){
            返回新LocalityList【尺寸】;
        }
    };

    公共无效writeToParcel(包裹DEST,INT标志){
        INT大小= this.size();

        //我们必须写列表的大小,我们需要他重新名单
        dest.writeInt(大小);

        的for(int i = 0; I<大小;我++){
            局部性R = this.get(ⅰ);

            dest.writeString(r.getDescription());
            dest.writeDouble(r.getLatitude());
            dest.writeDouble(r.getLongitude());
        }
    }
}
 

局部性的类

 进口android.os.Parcel;
进口android.os.Parcelable;


公共类局部性实现Parcelable {

    私人字符串mDescription;
    私人双人mLatitude;
    私人双人mLongitude;


    公共活动场所(字符串描述,双纬度,经度两倍){
        超();
        this.mDescription =描述;
        this.mLatitude =纬度;
        this.mLongitude =经度;
    }

    公共活动场所(){
        超();
    }


    公共字符串getDescription(){
        返回mDescription;
    }

    公共无效setDescription(字符串描述){
        this.mDescription =描述;
    }


    公共双getLatitude(){
        返回mLatitude;
    }

    公共无效setLatitude(双纬){
        this.mLatitude =纬度;
    }


    公共双getLongitude(){
        返回mLongitude;
    }

    公共无效setLongitude(双经度){
        this.mLongitude =经度;
    }


    @燮pressWarnings(未使用)
    公共活动场所(包裹中){
        本();
        readFromParcel(在);
    }

    私人无效readFromParcel(包裹中){
        this.mDescription = in.readString();
        this.mLatitude = in.readDouble();
        this.mLongitude = in.readDouble();
    }

    公众诠释describeContents(){
        返回0;
    }

    公众最终Parcelable.Creator<局部性> CREATOR =新Parcelable.Creator<局部性>(){
        公共活动场所createFromParcel(包裹中){
            返回新的地点(在);
        }

        公共活动场所[] newArray(INT尺寸){
            返回新局部性【尺寸】;
        }
    };


    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeString(mDescription);
        dest.writeDouble(mLatitude);
        dest.writeDouble(mLongitude);
    }
}
 

解决方案

是的,让地点类本身的 Parcelable 的,也不要忘了到的初始化

 的ArrayList<局部性> mList =新的ArrayList<局部性>();
 

Code now modified to reflect the accepted solution.This now serves as a working example of how to pass a custom ArrayList into a DialogFragment.

I am passing an ArrayList of custom objects to a DialogFragment using a Bundle on newInstance.The arraylist is received correctly in newInstance. The call to putParcelable executes fine (no errors), but putting breakpoints in the parcelable code in the ArrayList object shows that the parcel methods are not been called when setting or getting the data.

Am i correct creating a LocalityList class for the ArrayList and making that parcelable, or should Locality class itself be parcelable ?

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 class

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);
    }
}
解决方案

Yes, make Locality class itself Parcelable, and don't forgot to initialize

ArrayList<Locality> mList= new ArrayList<Locality>();

这篇关于安卓的ArrayList&LT;为MyObject&GT;通过为parcelable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:35