如何将bean类型数组列表(parceble)保存和检索到sharedPreference

时间:2016-03-30 11:17:05

标签: java android sharedpreferences

这是我的模型类

public class Details implements Parcelable {

    public String start;
    public String end;
    public long tymInSecond;
    public long time;
    public String haltNamne;
    public String arrTime;
    public long avgDuration;
    public double latitude;
    public double longitude;
    public Details(String start,String end,String haltNamne,String arrTime,long avgDuration,double lat,double log,long tymInSecond ){
        this.start=start;
        this.end=end;
        this.tymInSecond=tymInSecond;
        this.arrTime=arrTime;
        this.avgDuration=avgDuration;
        this.haltNamne=haltNamne;
       this.latitude=lat;
        this.longitude=log;
    }

    public Details(Parcel in) {
        readFromParcel(in);
    }


    public String getStart(){return  start;}
    public String getEnd(){return end;}
    public long gettymInSecond(){return tymInSecond;}
    public long getTime(){return time;}
    public String getHaltNamne(){return haltNamne;}
    public String getArrTime(){return arrTime;}
    public long getAvgDuration(){return avgDuration;}
    public double getLatitude(){return  latitude;}
    public double getLongitude(){return longitude;}

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(start);
        dest.writeString(end);
        dest.writeString(haltNamne);
        dest.writeDouble(latitude);
        dest.writeDouble(longitude);
        dest.writeString(arrTime);
        dest.writeLong(tymInSecond);
    }
    private void readFromParcel(Parcel in) {

        // We just need to read back each
        // field in the order that it was
        // written to the parcel
        end = in.readString();
        start = in.readString();
        haltNamne = in.readString();
        latitude = in.readDouble();
        longitude = in.readDouble();
        arrTime=in.readString();
        tymInSecond=in.readLong();
    }

    @SuppressWarnings("rawtypes")
    public static final Details.Creator CREATOR =
            new Parcelable.Creator() {
                public Details createFromParcel(Parcel in) {
                    return new Details(in);
                }

                public Details[] newArray(int size) {
                    return new Details[size];
                }
            };
}

这是我的其他代码!现在将我的“arrayListdetail”保存到sharedPreference并检索。

private void  parseJson(String jsonString) {

    try {
        JSONObject object=new JSONObject(jsonString);
        JSONObject average=object.getJSONObject("average");
        String start=average.getString("start");
        String end=average.getString("end");
        String totalTime=average.getString("completionTime");
        System.out.println("completionTime:+++++++++>>>>>>>>>>>"+totalTime);
        JSONArray halts=average.getJSONArray("halts");
        for(int i=0;i<halts.length(); i++){
            JSONObject obj=halts.getJSONObject(i);
            String haltName=obj.getString("name");
            tymInSecond=obj.getLong("arrTime");
            long avgDuration=obj.getLong("avgDuration");
            double lat=obj.getDouble("lat");
            double log=obj.getDouble("lng");
            addedtime=UtilClass.addTimeToCurrentTym((int) tymInSecond);
            Details d=new Details(start,end,haltName,addedtime,avgDuration,lat,log,tymInSecond);
            arrayListdetail.add(d);
}

0 个答案:

没有答案