使用SimpleDateFormat以字符串形式在SQLite上插入Firebase日期字段 [英] Inserting Firebase date field on SQLite as String with SimpleDateFormat

查看:57
本文介绍了使用SimpleDateFormat以字符串形式在SQLite上插入Firebase日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Firebase收到一个孩子更改,但是当我插入SQLite时,它会给我一个警告.插入成功,但是如何解决此警告?

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-所提供的只是:

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

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

数据库控制器

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;
}

这是一些代码.

Passageiros.java(实体)

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架构

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.

推荐答案

您似乎正在使用此Firebase对象:

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
}

要映射到此字段:

private Date data_validade;

警告来自以下事实:Date没有setDay()和setTimezoneOffset()来匹配对象中的那些字段.确实,这不是在Firebase中存储日期的最佳方法.

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.

如果要将日期存储在Firebase中,请使用长整数.如果随后需要将其转换为日期,则只需使用"new 日期(长)".如果您需要将Date转换为long,请使用 date.getTime().

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

这篇关于使用SimpleDateFormat以字符串形式在SQLite上插入Firebase日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆