Inserting Firebase date field on SQLite as String with SimpleDateFormat

徘徊边缘 提交于 2020-01-25 17:11:54

问题


I am receiving a child change from Firebase, but when I insert into SQLite, it gives me a warning. The insertion was success, but how do I fix this warning?

W/ClassMapper: No setter/field for day found on class java.util.Date (fields/setters are case sensitive!)
W/ClassMapper: No setter/field for timezoneOffset found on class java.util.Date (fields/setters are case sensitive!)

Firebase ChildEventListener - Just the piece that is giving:

@Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.d(TAG, "item adionado :" + dataSnapshot.getValue(Passageiros.class).toString());
            String retorno = crud.inserirDados(dataSnapshot.getValue(Passageiros.class));
            if(retorno.equals("Registro inserrido")){
                mListaPassageiros.add(dataSnapshot.getValue(Passageiros.class));
            }
        }

This is the line that I presume is giving the error:

crud.inserirDados(dataSnapshot.getValue(Passageiros.class));

The DB Controller

public String inserirDados(Passageiros passageiro) {
    ContentValues valores;
    long resultado = -10;
    if (carregarDadosByChave(passageiro.getChave()).getCount() <= 0) {
        db = passageirosDAO.getWritableDatabase();
        valores = new ContentValues();
        valores.put(Passageiros.PassageirosEntry.COLUMN_NAME_NOME, passageiro.getNome());
        valores.put(Passageiros.PassageirosEntry.COLUMN_NAME_CHAVE, passageiro.getChave());
        valores.put(Passageiros.PassageirosEntry.COLUMN_NAME_DATA_VALIDADE, sdf.format(passageiro.getData_validade()));

        resultado = db.insert(Passageiros.PassageirosEntry.TABLE_NAME, null, valores);
        db.close();
    }if (resultado == -10){
        return "Dado não inserido";
    }else if (resultado == -1) {
        return "Erro ao inserrir registro";
    } else {
        return "Registro inserrido";
    }
}
public Cursor carregarDadosByChave(String chave) {
    Cursor cursor;
    String[] campos = {Passageiros.PassageirosEntry.COLUMN_NAME_CHAVE, Passageiros.PassageirosEntry.COLUMN_NAME_NOME, Passageiros.PassageirosEntry.COLUMN_NAME_DATA_VALIDADE, Passageiros.PassageirosEntry._ID};
    String where = Passageiros.PassageirosEntry.COLUMN_NAME_CHAVE + " LIKE '" + chave + "'";
    db = passageirosDAO.getReadableDatabase();
    cursor = db.query(Passageiros.PassageirosEntry.TABLE_NAME, campos, where, null, null, null, null, null);

    if (cursor != null) {
        cursor.moveToFirst();
    }
    db.close();
    return cursor;
}

Here is some code.

Passageiros.java (Entity)

public class Passageiros implements Serializable {
public String objectId;
private String nome;
private Date data_validade;
private String chave;
private Boolean ativo;
private Date created;
private Date updated;

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public Date getData_validade() {
    return data_validade;
}

public void setData_validade(Date data_validade) {
    this.data_validade = data_validade;
}

public String getChave() {
    return chave;
}

public void setChave(String chave) {
    this.chave = chave;
}

public Boolean getAtivo() {
    return ativo;
}

public void setAtivo(Boolean ativo) {
    this.ativo = ativo;
}

public Passageiros(String nome, Date data_validade, String chave, Boolean ativo) {
    this.nome = nome;
    this.data_validade = data_validade;
    this.chave = chave;
    this.ativo = ativo;
}
public Passageiros(){

}

@Override
public String toString() {
    return "Passageiros{" +
            "objectId='" + objectId + '\'' +
            ", nome='" + nome + '\'' +
            ", data_validade=" + data_validade +
            ", chave='" + chave + '\'' +
            ", ativo=" + ativo +
            ", created=" + created +
            ", updated=" + updated +
            '}';
}

public static class PassageirosEntry implements BaseColumns{
    public static final String TABLE_NAME = "passageiros";
    public static final String COLUMN_NAME_NOME = "nome";
    public static final String COLUMN_NAME_DATA_VALIDADE = "data_validade";
    public static final String COLUMN_NAME_CHAVE = "chave";
}
}

Firebase architecture with values

"-KbeD42PVfdKIoHXuGTV" : {
  "chave" : "-KbeD42PVfdKIoHXuGTV",
  "data_validade" : {
    "date" : 29,
    "day" : 0,
    "hours" : 10,
    "minutes" : 34,
    "month" : 0,
    "seconds" : 36,
    "time" : 1485693276834,
    "timezoneOffset" : 120,
    "year" : 117
  },
  "nome" : "Rogerio4"
}

If you need more details, let me know.


回答1:


It looks like you're expecting this Firebase object:

"data_validade" : {
  "date" : 29,
  "day" : 0,
  "hours" : 10,
  "minutes" : 34,
  "month" : 0,
  "seconds" : 36,
  "time" : 1485693276834,
  "timezoneOffset" : 120,
  "year" : 117
}

To map to this field:

private Date data_validade;

The warnings come from the fact that Date has no setDay() and no setTimezoneOffset() to match those fields in the object. Really, this is not the best way to store a date in Firebase.

If you want to store a date in Firebase, just use a long integer. If you then need to convert that to a Date, just create a new Date with "new Date(long)". If you need to convert a Date into a long, use date.getTime().



来源:https://stackoverflow.com/questions/42047421/inserting-firebase-date-field-on-sqlite-as-string-with-simpledateformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!