我的代码只从我的数据库的第一个字段检索数据。请看细节

时间:2016-06-02 05:59:42

标签: java android firebase firebase-realtime-database firebase-hosting

我编写了一些从firebase数据库中检索`数据的代码。

问题是我的代码只从数据库中的第一个数据字段而不是从所有字段中检索数据。

我希望它从我的数据库的每个字段中检索数据。

这是我的代码:

retrieveRef = new Firebase("https://appname.firebaseio.com/hDetails/");

retrieveRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        hDescription = (String) dataSnapshot.child("hDescription").getValue();
        hImage = (String) dataSnapshot.child("hImage").getValue();
        hLat = (String) dataSnapshot.child("hLat").getValue();
        hLng = (String) dataSnapshot.child("hLng").getValue();
        postedAtTime = (String) dataSnapshot.child("postedAtTime").getValue();
        postedBy = (String) dataSnapshot.child("postedBy").getValue();
        postedOnDate = (String) dataSnapshot.child("postedOnDate").getValue();
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Toast.makeText(getBaseContext(), firebaseError.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
});

这是我的数据库的图像:

enter image description here

仅从最后一个字段检索数据,即每次f4452618-a073-4868-a9c6-050b055b4861

此处build.gradle(模块:应用):

    apply plugin: 'com.android.application'

    android {

        compileSdkVersion 23
        buildToolsVersion "23.0.2"
        defaultConfig {
            applicationId "com.abc.xyz"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 5
            versionName "0.4"
            multiDexEnabled true;
        }
        packagingOptions {
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE-FIREBASE.txt'
            exclude 'META-INF/NOTICE'
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.config
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile 'com.android.support:design:23.4.0'
        compile 'com.android.support:cardview-v7:23.4.0'
        compile 'com.android.support:recyclerview-v7:23.4.0'
        compile 'com.firebase:firebase-client-android:2.3.1'
        compile 'com.firebaseui:firebase-ui:0.3.1'
        compile 'com.facebook.android:facebook-android-sdk:4.1.0'
        compile 'com.google.android.gms:play-services-maps:9.0.0'
        compile 'com.google.android.gms:play-services-plus:9.0.0'
        compile 'com.android.support:support-v4:23.4.0'
        compile 'com.squareup.picasso:picasso:2.5.2'
        compile 'com.google.android.gms:play-services-location:9.0.0'
        compile 'com.firebase:geofire:1.1.0'
        compile 'com.google.firebase:firebase-database:9.0.1'
        compile 'io.github.yavski:fab-speed-dial:1.0.4'
        compile 'com.google.gms:google-services:3.0.0'
    }

apply plugin: 'com.google.gms.google-services'

此处buid.gradle(项目:项目名称):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这里出了什么问题?

请告诉我。

抱歉,如果问题似乎格式错误。我还是初学者。

2 个答案:

答案 0 :(得分:0)

尝试使用Firebase中的Query类

 DatabaseReference root = FirebaseDatabase.getInstance().
 getReferenceFromUrl("https://appname.firebaseio.com/hDetails/");

 Query playlistQuery = root.orderByKey();

 playlistQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s)
            {

                //Make sure you have a empty constructor to de-serialize it

                YourPOJO playlist = dataSnapshot.getValue(YourPOJO.class);
                playlist.getName(); // Depends on the field names
                playlist.getDetails();
                ....
            }
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

答案 1 :(得分:0)

这将获取hDetails列表:

FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference retrieveRef = database.getReference("hDetails");
    final List<YourPOJO> pojoList = new ArrayList<>();
    retrieveRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            pojoList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                pojoList.add(snapshot.getValue(YourPOJO.class));
            }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Toast.makeText(getBaseContext(), firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

在顶级build.gradle上添加的依赖项:

dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:3.0.0'
    } 

App level build.gradle:

dependencies {
    compile 'com.google.firebase:firebase-core:9.0.0'
    compile 'com.google.firebase:firebase-auth:9.0.0'
    compile 'com.google.firebase:firebase-database:9.0.0'
}
apply plugin: 'com.google.gms.google-services'