我无法从Firebase检索我的数据

时间:2017-10-24 09:36:28

标签: java android firebase firebase-realtime-database

这是我的Firebase树:

package com.example.android.frep;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class fHome extends AppCompatActivity {

List<resepNusantara> listResepNusantara;
ListView listViewResep;

DatabaseReference dbResepNusantara;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_f_home);
    listViewResep = (ListView) findViewById(R.id.resepList);
    dbResepNusantara = FirebaseDatabase.getInstance().getReference("resepNusantara");
}


@Override
protected void onStart() {
    super.onStart();

    dbResepNusantara.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //clear the list
            //listResepNusantara.clear();

            //iterating all nodes
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                //getting resep
                //This is the ERROR START
                resepNusantara resepN = postSnapshot.getValue(resepNusantara.class);
                //adding resep to the list
                listResepNusantara.add(resepN);
            }

            //creating the adapter for the list
            ResepList resepListAdapter = new ResepList(fHome.this, listResepNusantara);
            //attaching adapter to the listView
            listViewResep.setAdapter(resepListAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

}

嗨,我想问的每个人,我都有从firebase检索数据的问题

在我的模型类resepNusantara中,我试图将我的属性类型更改为Long,但仍然是错误。错误行在评论

我遵循以下准则:https://www.simplifiedcoding.net/firebase-realtime-database-crud/

这是LogCat:

10-24 20:35:14.002 18631-18745/com.example.android.frep D/libc-netbsd: [getaddrinfo]:  hostname=frep-ffacb.firebaseio.com;  servname=(null);  app_pid=18631;  app_uid=10161;  ai_flags=1024;  ai_family=0; ai_socktype=1; netid=0; mark=0; from prox result 0
10-24 20:35:14.521 18631-18745/com.example.android.frep E/NativeCrypto: ssl=0x7f93ff4880 cert_verify_callback x509_store_ctx=0x7f6d5889f0 arg=0x0
10-24 20:35:15.610 18631-18631/com.example.android.frep E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.android.frep, PID: 18631
  com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
      at com.google.android.gms.internal.zzbqi.zzaD(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
      at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
      at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
      at com.example.android.frep.fHome$1.onDataChange(fHome.java:56)
      at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
      at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
      at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:815)
      at android.os.Handler.dispatchMessage(Handler.java:104)
      at android.os.Looper.loop(Looper.java:238)
      at android.app.ActivityThread.main(ActivityThread.java:6006)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)

1 个答案:

答案 0 :(得分:1)

编组resepNusantara类时,数据类型不匹配。具体来说,其中一个成员在数据库中存储为long,在类中存储为string

将对象转储到控制台并检查数据库以确保所有类型都已同步。

有关详情,请参阅此帖子:Firebase DatabaseException: Failed to convert value of type java.lang.Long to String