0

I'm having trouble retrieving data from a Firebase database. I've implemented all the necessary dependencies, there is no error by the retrieved data isn't showing.

I've tried so many youtube videos, implemented the necessary dependencies and the app runs smoothly but the data is not being retrieved.

HERE IS THE CODE OF MAIN ACTIVITY:

    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

AND HERE'S THE ORDERS' CLASS:

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

the 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'
    }

this is the 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
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This **[answer](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)** might help. – Alex Mamo Jun 22 '20 at 07:55

1 Answers1

0

The first fix:

You are using toString() for the Orders objects you receive from the Firebase's ChildEventListener callback onChildAdded() while not overriding toString() of the Orders class; so it calls the super class's toString().

String value = dataSnapshot.getValue(Orders.class).toString();

So you need to override the toString() method of the Orders to return something that you need to show up on the ListView row item.

The second fix:

you didn't attach the arrayList to the arrayAdapter, so you need to replace

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item);

with

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, arrayList);
Zain
  • 37,492
  • 7
  • 60
  • 84