尝试将数据检索到自定义类时,Firebase会引发数据库异常

时间:2018-04-21 04:51:32

标签: android firebase-realtime-database

尝试从firebase检索数据时出现以下错误 com.google.firebase.database.DatabaseException:无法将java.lang.Long类型的对象转换为com.ui.controls.CodeDescMast类型。

我的POJO类类包含一个int和string。我已经定义了空构造函数,getter / setter方法,所有这些都是公共的。

CodeDescMast cdeDescMast = dSnpSht.getValue(CodeDescMast.class)正在产生数据库异常错误。

问题是我使用相同的类来保存和检索,所以为什么会出现这个错误。

我的POJO课程

public class CodeDescMast {
int code;
String desc;

public CodeDescMast() { }
public CodeDescMast(int code, String desc) {
    this.code = code;
    this.desc = desc;
}

public int getCode() {
    return code;
}

public void setCode(int code) {
    this.code = code;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

}

Firebase助手类

public class FbaseHelper {

DatabaseReference dbRef;
Boolean saved= null; 
CodeDescMast codeDescMast;
ArrayList<CodeDescMast> cdeDescAryList;

public FbaseHelper() { }
public FbaseHelper(DatabaseReference refDb) {
    this.dbRef = refDb;
    cdeDescAryList = new ArrayList<CodeDescMast>() ;
}

public void read(Context context) {

    String csvLine;

    InputStream is=null ;
    try {
        is = context.getAssets().open("b.csv") ;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is)) ;
        codeDescMast = new CodeDescMast();
        while((csvLine = reader.readLine())!=null) {
           String[] row = csvLine.split(",") ;
           if (! row[1].equals("Desc")) {

               codeDescMast.setCode(Integer.valueOf(row[0]));
               codeDescMast.setDesc(row[1]);
               fBaseSave();

           }
        }

        is.close();

    } catch(IOException ioex) {
        ioex.printStackTrace();

    }
}
public Boolean fBaseSave() {
    try {
        dbRef.push().setValue(codeDescMast) ;
        saved = true;

    }catch (DatabaseException dbEx) {
        dbEx.printStackTrace();
        saved = false ;

    }

    return saved;
}

从Firebase中检索

  public ChildEventListener chldEvtListner = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        for(DataSnapshot dSnpSht : dataSnapshot.getChildren()) {
            long a = dataSnapshot.getChildrenCount();

            CodeDescMast cdeDescMast = dSnpSht.getValue(CodeDescMast.class) ;

            cdeDescAryList.add(cdeDescMast) ;
        }
    }

来自Firebase的JSON

{
  "-LAXQwrXU01IA5ha5eBW" : {
  "code" : 602,
  "desc" : "xxxxxxxxxxx"
},
 "-LAXQwrezdMRpfVdEVY8" : {
 "code" : 603,
 "desc" : "xxxxxxx"
}
}

1 个答案:

答案 0 :(得分:0)

创建数据库引用

公共FirebaseDatabase数据库= FirebaseDatabase.getInstance();

public DatabaseReference users = database.getReference(“Node”)。child(“您的ID或firebase uid引用”);

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot dSnpSht: dataSnapshot.getChildren()) 
     {
      CodeDescMast cdeDescMast = dSnpSht.getValue(CodeDescMast.class);             

      }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) { }
});

///试试这个