简体   繁体   中英

My code is retrieving data only from first field of my database. Please see details

I have written some code which retrieves` data from my firebase database.

The problem is that my code is retrieving data only from the very first field of data in my database and not from all the fields.

I want it to retrieve the data from each and every field of my database.

Here's my code:

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

Here's an image of my database:

在此处输入图片说明

The data is getting retrieved only from the very last field, ie f4452618-a073-4868-a9c6-050b055b4861 every time.

Here's build.gradle (module:app):

    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'

Here's buid.gradle (project: projectname):

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
}

What's going wrong here?

Please let me know.

Sorry, if question seems to be badly formatted. I'm still a beginner here.

Try using the Query class from Firebase

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

            }
        });

This will fetch list of 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();
        }
    });

Dependencies to add on Top level 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM