从Firebase android获取特定数据

时间:2016-12-24 08:02:48

标签: android firebase firebase-realtime-database

我一直在尝试从Firebase获取数据。这是数据库结构:

下面的数据库快照

enter image description here

我想只检索PartyName和ID。我的代码是这样的:

    listView = (ListView) findViewById(R.id.listView);

    Firebase.setAndroidContext(this);
    firebase = new Firebase("https://login-b3a11.firebaseio.com/Users");
    //  com.firebase.client.Query query=firebase.orderByChild("Date").equalTo("ID");
    listView = (ListView) findViewById(R.id.listView);
    final ArrayAdapter arrayAdapter = new ArrayAdapter(Status.this, android.R.layout.simple_list_item_1, listGet);
    listView.setAdapter(arrayAdapter);
    firebase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            for(DataSnapshot ds:dataSnapshot.getChildren()){
                Get g= (Get) ds.getValue();
                listGet.add(g.getPartyName());
                arrayAdapter.notifyDataSetChanged();
            }
        }

Get是一个具有getter和setter方法的类。但是我的应用程序崩溃了。

logcat的

12-24 13:40:11.010 2134-3707/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=490, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
12-24 13:40:11.132 2134-3707/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=491, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
12-24 13:40:11.330 20641-20641/? E/FirebaseApp: Firebase API initialization failure.
                                                java.lang.reflect.InvocationTargetException
                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                    at com.google.firebase.FirebaseApp.zza(Unknown Source)
                                                    at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                    at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                    at com.google.firebase.FirebaseApp.zzeh(Unknown Source)
                                                    at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
                                                    at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
                                                    at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
                                                    at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
                                                    at android.app.ActivityThread.installProvider(ActivityThread.java:5174)
                                                    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769)
                                                    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709)
                                                    at android.app.ActivityThread.-wrap1(ActivityThread.java)
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415)
                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                    at android.os.Looper.loop(Looper.java:148)
                                                    at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                 Caused by: java.lang.NoSuchMethodError: No static method zzz(Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzaa; or its super classes (declaration of 'com.google.android.gms.common.internal.zzaa' appears in /data/app/com.example.amisha.login-2/base.apk)
                                                    at com.google.android.gms.measurement.internal.zzx.zzbd(Unknown Source)
                                                    at com.google.android.gms.measurement.AppMeasurement.getInstance(Unknown Source)
                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                    at com.google.firebase.FirebaseApp.zza(Unknown Source) 
                                                    at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                    at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                    at com.google.firebase.FirebaseApp.zzeh(Unknown Source) 
                                                    at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source) 
                                                    at android.content.ContentProvider.attachInfo(ContentProvider.java:1748) 
                                                    at android.content.ContentProvider.attachInfo(ContentProvider.java:1723) 
                                                    at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) 
                                                    at android.app.ActivityThread.installProvider(ActivityThread.java:5174) 
                                                    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769) 
                                                    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709) 
                                                    at android.app.ActivityThread.-wrap1(ActivityThread.java) 
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415) 
                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                    at android.os.Looper.loop(Looper.java:148) 
                                                    at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
12-24 13:40:11.513 20672-20672/? E/NEW_BHD: Battery Power 

5 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

首先,您必须以正确的方式引用您的数据库。从代码中删除这两行:

Firebase.setAndroidContext(this);
firebase = new Firebase("https://login-b3a11.firebaseio.com/Users");
//  com.firebase.client.Query 

用这个替换它们

FirebaseDatbase mFirebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference mMessagesDatabaseReference=mFirebaseDatabase.getReference();

现在,您可以使用名为DatabaseReference的{​​{1}}对象发送您的查询。

答案 2 :(得分:0)

 FATAL EXCEPTION: main
                                                 Process: com.example.amisha.login, PID: 4849
                                                 java.lang.ClassCastException: java.lang.String cannot be cast to com.example.amisha.login.Get
                                                     at com.example.amisha.login.Status$1.onChildAdded(Status.java:52)
                                                     at com.google.android.gms.internal.zzahh.zza(Unknown Source)
                                                     at com.google.android.gms.internal.zzajh.zzctc(Unknown Source)
                                                     at com.google.android.gms.internal.zzajk$1.run(Unknown Source)
                                                     at android.os.Handler.handleCallback(Handler.java:746)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-24 16:05:25.710 5445-5445/? E/NEW_BHD: Battery Power Supply logging Daemon start!!!!!
12-24 16:05:25.711 5445-5445/? E/NEW_BHD: Cannot run on production devices!
12-24 16:05:30.734 5463-5463/? E/NEW_BHD: Battery Power Supply logging Daemon start!!!!!
12-24 16:05:30.735 5463-5463/? E/NEW_BHD: Cannot run on production devices!
12-24 16:05:35.752 5464-5464/? E/NEW_BHD: Battery Power Supply logging Daemon start!!!!!
12-24 16:05:35.753 5464-5464/? E/NEW_BHD: Cannot run on production devices!
12-24 16:05:36.425 2134-3707/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=512, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
12-24 16:05:37.045 5489-5489/? E/Finsky: [1] com.google.android.finsky.wear.bl.a(847): onConnectionFailed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
12-24 16:05:37.351 2134-4459/? E/LocSvc_IzatApiV02: E/virtual int izat_core::IzatApiV02::getBatchSize(int32_t):620]: Error : st = 4, ind.status = -1600126925
12-24 16:05:37.351 2134-4459/? E/LocSvc_IzatApiV02: E/virtual int izat_core::IzatApiV02::getBatchSize(int32_t):633]: get batching size failed.Or modem does not support batching 
12-24 16:05:37.507 2134-3707/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=514, legacyType=-1, [ Capabilities: INTERNET&NOT_

我现在得到了这个。尝试的代码是:

 listView = (ListView) findViewById(R.id.listView);
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        mDatabase=FirebaseDatabase.getInstance().getReferenceFromUrl("https://login-b3a11.firebaseio.com/Users");
        listView = (ListView) findViewById(R.id.listView);
       final ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, listGet);
        listView.setAdapter(arrayAdapter);
       mDatabase.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
           @Override
           public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
               for(com.google.firebase.database.DataSnapshot ds:dataSnapshot.getChildren()){
                   Get g=new Get();
                   g=(Get)ds.getValue();
                   myadapter.add(g.getPartyName());
                   arrayAdapter.notifyDataSetChanged();
               }
           }

答案 3 :(得分:0)

问题是你是以错误的方式投射它。在onChildAdded中执行此操作

 @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        for(DataSnapshot ds:dataSnapshot.getChildren()){
            Get g= ds.getValue(Get.class);
            listGet.add(g.getPartyName());
            arrayAdapter.notifyDataSetChanged();
        }
    }

答案 4 :(得分:0)

FirebaseDatabase database ;

instanse在onCreate上创建

database = FirebaseDatabase.getInstance();

DatabaseReference dbOrderReference = database.getReference("Users/" +"001");
dbOrderReference.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(final DataSnapshot dataSnapshot) {
  //get id and phone_number
  String id=String.valueOf(dataSnapshot.child("id").getValue());
  String phone_num=String.valueOf(dataSnapshot.child("PhoneNo").getValue());

 }
 @Override
 public void onCancelled(DatabaseError databaseError) {

 }
});