문제

I am creating a class that Implements the Parcelable,.

public class PosicaoResumoMobile implements Parcelable {

private Float _Latitude;
private Float _Longitude;
private org.joda.time.DateTime _DataHora;
    ...

But this class has an attribute of type org.joda.time.DateTime. How can I write this attribute in the following method implementing the Parcelable since it is not possible out.writeDateTime (_DataHora).

@Override
public void writeToParcel(Parcel out, int flags) 
{
    //TODO: How to write org.joda.time.DateTime 
    out.writeFloat(_Latitude);
    out.writeFloat(_Longitude);
}

and read

private PosicaoResumoMobile(Parcel in){
    //TODO: How to read org.joda.time.DateTime 
    Float latitude = in.readFloat();
    Float longitude = in.readFloat();
}
도움이 되었습니까?

해결책

You should be able to get the milliseconds using getMillis()

// to write to parcel
out.writeFloat(_Latitude);
out.writeLong(jodaDTInstance.getMillis())

// to read from parcel
Float longitude = in.readFloat();
jodaDTInstance = new DateTime(in.readLong());

다른 팁

Based on @artworkad comment, if you need to include Timezone information, you will have to use toString() and DateTime.parse():

To write to parcel:

dest.writeString(mDateTime.toString());

To read from parcel:

mDateTime = DateTime.parse(in.readString());

tray this code kotlin :

write :

out.writeSerializable(myDateObject)

read :

myDateObject =  in.readSerializable() as Date
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top