0

i have learned that using global static variable is NOT good engineering, so i try to always return the value or object what i need. In this case, how can i return the arraylist when all the data finally synced?

here's my code below:

public List<String> getList() {

DatabaseReference dbRefList = fireDB.getReference("database/list");
dbRefList.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

    List<String> name = new ArrayList<>();
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
      String name = ds.child("name").getValue().toString();
      versionName.add(name);
    }
    dataSnapshot.child("name").getValue().toString();
    //want to return the list
  }

  @Override
  public void onCancelled(DatabaseError databaseError) { }
});
}

Edit

here's my follow problem using CallBack: the code below i get null of the callback

public void downloadImagesFromFireStorage(String imgName, final BitmapCallBackInterface bitmapCallBackInterface) {

StorageReference storageRef = FirebaseStorage.getInstance()
        .getReferenceFromUrl("gs://xxxxx.appspot.com").child("imgs").child(imgName + ".png");

final long ONE_MEGABYTE = 1024 * 1024;

storageRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {

  @Override
  public void onSuccess(byte[] bytes) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    bitmapCallBackInterface.onCallBack(bitmap);
  }
});

when i call

downloadImagesFromFireStorage(imgName, new BitmapCallBackInterface() {
      @Override
      public void onCallBack(Bitmap bitmap) {
        insertImg(couponName, bitmap);
      }
    });

am i wrong with some process?

Hayate
  • 143
  • 6
  • You need to create some kind of callback, preferably one that can handle the error also. – Heinrisch Jan 30 '18 at 11:34
  • @Heinrisch such as Futures.addCallBack? – Hayate Jan 30 '18 at 11:36
  • Or implement your own `public class Callback { onSuccess() onFailure() }` – Heinrisch Jan 30 '18 at 12:08
  • See the last part of my answer from the duplicate question. Get that list outs of `onDataChange()` method, using the callback. Will solve your problem for sure. – Alex Mamo Jan 30 '18 at 13:19
  • @AlexMamo To be sure, in your example, when call the readData method, is that the thread will run next expression after the callback? – Hayate Jan 30 '18 at 13:31
  • Yes, it's a separate thread. – Alex Mamo Jan 30 '18 at 13:42
  • @AlexMamo i have edited my question since i tried to use the callBack on returning other object. please take a look :) – Hayate Jan 30 '18 at 16:28
  • No, it's not correct. Just try to implement the exact stept I explained there. – Alex Mamo Jan 30 '18 at 16:30
  • @AlexMamo This is what i do: 1, create an Interface that the parameter of onCallBack is a Bitmap. 2, modify my method that call onCallBack when download complete. 3, call the method and override onCallBack. I couldn't figure out where i miss QQ. Would you like to explain the wrong place? – Hayate Jan 30 '18 at 16:44
  • If you have other issues regarding this solution, post another fresh question so me or other users can help you. – Alex Mamo Jan 30 '18 at 16:47

1 Answers1

0

Try using EventBus. Check the link

Eby Cloudins
  • 111
  • 8