有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java My代码仅从数据库的第一个字段检索数据。请查看详细信息

我写了一些代码,从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(模块:app):

    apply plugin: 'com.安卓.application'

    安卓 {

        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-安卓.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.config
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.安卓.support:appcompat-v7:23.4.0'
        compile 'com.安卓.support:design:23.4.0'
        compile 'com.安卓.support:cardview-v7:23.4.0'
        compile 'com.安卓.support:recyclerview-v7:23.4.0'
        compile 'com.firebase:firebase-client-安卓:2.3.1'
        compile 'com.firebaseui:firebase-ui:0.3.1'
        compile 'com.facebook.安卓:facebook-安卓-sdk:4.1.0'
        compile 'com.google.安卓.gms:play-services-maps:9.0.0'
        compile 'com.google.安卓.gms:play-services-plus:9.0.0'
        compile 'com.安卓.support:support-v4:23.4.0'
        compile 'com.squareup.picasso:picasso:2.5.2'
        compile 'com.google.安卓.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(项目名称:projectname):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.安卓.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) 个答案

  1. # 1 楼答案

    这将获取hd详细信息列表:

    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 {
            classpath 'com.android.tools.build:gradle:2.1.0'
            classpath 'com.google.gms:google-services:3.0.0'
        } 
    

    应用级构建。格雷德尔:

    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'
    
  2. # 2 楼答案

    尝试使用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) {
    
                }
            });