Pablo R.
Pablo R.

Reputation: 723

OnDataChange Firebase Android Listener

I' am trying to retrieve data from firebase database, this method is called when you enter to the app and never listens OnDataChange. If I upload a new image to firebase, OnDataChange is called and shows the last url I have introduced. I want to retrieve all data urls but I thought this method is always called no matter is new data on the realtime database. Thank you in advice.

    private void prepareAlbums() {

    // storageReference = FirebaseStorage.getInstance().getReference().child("Images").child(FirebaseAuth.getInstance().getCurrentUser().toString());

    System.out.print("user "+ firebaseAuth.getCurrentUser().getUid());
    databaseReference.getRoot().child("user-images")
        .child(firebaseAuth.getCurrentUser().getUid())
        .addValueEventListener(new ValueEventListener() {
          @Override public void onDataChange(DataSnapshot dataSnapshot) {
            Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
            if (td != null) {
              Iterator iterator = td.entrySet().iterator();
              while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String url = (String) entry.getValue();
                System.out.print("url "+ url);
                urlArray.add(url);
              }
            }
          }

          @Override public void onCancelled(DatabaseError databaseError) {

          }
        });

    CardImage a;
    for (String s : urlArray) {
      a = new CardImage("usuario1", 3, s, "madrid");
      albumList.adenter code hered(a);
    }

    adapter.notifyDataSetChanged();
  }

This is my Database Structure

spear-e5a6a
  user-images
   3cAhNM75JdaeojrJ5RSjpBhonu63  // userid
   -KjN5Jr31u5lPwubAwEF: 
     "https://firebasestorage.googleapis.com/v0/b/spe..."
   -KjN5Nv0Sk8sC6dqDgrq: 
     "https://firebasestorage.googleapis.com/v0/b/spe..."

I solved the problem like this

databaseReference.getRoot().child("images")
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        imageArray.clear();
                        cardList.clear();
                        for (DataSnapshot imageSnapshot : dataSnapshot.getChildren()) {
                            Log.d(TAG, "onDataChange: " + imageSnapshot.getKey());
                            ImageInfo image = imageSnapshot.getValue(ImageInfo.class);
                            imageArray.add(image);

                        }
                        render(imageArray);

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }

                });


 private void render(ArrayList<ImageInfo> imgInfo) {
        final ArrayList<UserInfo> userArray = new ArrayList<>();


        CardImage card;
        for (ImageInfo imageInfo : imgInfo) {
            card = new CardImage(imageInfo.getName(), imageInfo.getRating(), imageInfo.getUrl(), imageInfo.getProvince(), imageInfo.getTimeStamp());
            cardList.add(card);
        }


        adapter.notifyDataSetChanged();

    }

images
  1494101684644:  //timestamp
  1494101402668
    comentary: 
    name: 
    province: 
    rating: 
    timeStamp: 
    url: 
    usermail: 
    voted: 
users
   //user id YMlB6t3Yp1Rs2mcpc9MstUSVD5z2:
   email: 
   images
   1494101402668:  timestamp
   1494101684644: 
   name: 

I don't really know if its the proper way.

Upvotes: 2

Views: 10252

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

Add the declaration of your urlArray in your onDataChange() method. Otherwise is null because this method is executed asynchronously.

For other approach, please visit this post and this post.

Hope it helps.

Upvotes: 1

Related Questions