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?