无法将数据从Firebase检索到ListView

时间:2020-06-20 13:28:46

标签: java android firebase firebase-realtime-database

我无法从Firebase数据库检索数据。我已经实现了所有必要的依赖关系,由于未显示检索到的数据,因此没有错误。

我尝试了许多youtube视频,实现了必要的依赖关系,并且应用程序运行平稳,但未检索到数据。

这里是主要活动的代码:

    public class OrdersActivity extends AppCompatActivity {
        ListView mListView;
        DatabaseReference dbRef;
        ArrayList<String> arrayList = new ArrayList<>();
        ArrayAdapter<String> arrayAdapter;
        private static final String TAG = "OrdersActivity";
    
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_orders);
            Log.d(TAG, "onCreate: Started");
            mListView = (ListView) findViewById(R.id.listView);
            dbRef = FirebaseDatabase.getInstance().getReference("Orders");
            arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item);
            mListView.setAdapter(arrayAdapter);
            dbRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded (@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    String value = dataSnapshot.getValue(Orders.class).toString();
                    arrayList.add(value);
                    arrayAdapter.notifyDataSetChanged();
                }
    
                @Override
                public void onChildChanged (@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    
                }
    
                @Override
                public void onChildRemoved (@NonNull DataSnapshot dataSnapshot) {
    
                }
    
                @Override
                public void onChildMoved (@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    
                }
    
                @Override
                public void onCancelled (@NonNull DatabaseError databaseError) {
    
                }
    
            });
        }
    }

enter image description here] 1

这是订单的类别:

    public class Orders {
        private String emri;
        private String data;
        private String statusi;
        private String imgURL;
    
        public Orders (String emri, String data, String statusi, String imgURL) {
            this.emri = emri;
            this.data = data;
            this.statusi = statusi;
            this.imgURL = imgURL;
        }
    
        public String getEmri () {
            return emri;
        }
    
        public void setEmri (String emri) {
            this.emri = emri;
        }
    
        public String getData () {
            return data;
        }
    
        public void setData (String data) {
            this.data = data;
        }
    
        public String getStatusi () {
            return statusi;
        }
    
        public void setStatusi (String statusi) {
            this.statusi = statusi;
        }
    
        public String getImgURL () {
            return imgURL;
        }
    
        public void setImgURL (String imgURL) {
            this.imgURL = imgURL;
        }
    }

BUILD.GRADLE(:APP)

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    
    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.3"
        defaultConfig {
            applicationId "com.fiek.projektiioc"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'com.google.firebase:firebase-auth:19.3.1'
        implementation 'com.google.android.material:material:1.0.0'
        implementation 'com.android.support:multidex:1.0.3'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'com.google.android.gms:play-services-maps:17.0.0'
        implementation 'androidx.cardview:cardview:1.0.0'
        testImplementation 'junit:junit:4.12'
        implementation 'com.firebaseui:firebase-ui-firestore:4.3.1'
        implementation 'com.google.firebase:firebase-firestore:21.4.3'
        implementation 'com.google.firebase:firebase-analytics:17.4.3'
        implementation 'com.google.firebase:firebase-database:19.3.1'
        implementation 'com.google.firebase:firebase-messaging:20.2.1'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }

这是BUILD.GRADLE(PROJECT)

    buildscript {
        repositories {
            google()
            jcenter()
            
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:4.0.0'
            classpath 'com.google.gms:google-services:4.3.3'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
            
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

1 个答案:

答案 0 :(得分:0)

第一个修复程序:

您正在将toString()用于从Firebase的Orders回调ChildEventListener收到的onChildAdded()对象,而不会覆盖toString()的{​​{1}}类;因此它称为超类的Orders

toString()

因此,您需要覆盖String value = dataSnapshot.getValue(Orders.class).toString(); 的{​​{1}}方法,以返回需要显示在toString()行项目上的内容。

第二个解决方法:

您没有将Orders附加到ListView上,因此需要替换

arrayList

使用

arrayAdapter