Pergunta

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();
}
Foi útil?

Solução

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());

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top