private Firebase_Database DbOnline;
ArrayList<ClassModel> clsList;
clsList = DbOnline.getClassesList();//return arraylist containing objects ...
//Implementation of getClassesList() in Firebase_Database CLASS..
public ArrayList<ClassModel> getClassesList(){//upto to this every thing execute but from here the //execution jumps to if(condition) line below...and I get null arraylist in return
FbDb.child("Classes").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren())
{
ClassModel classModel = ds.getValue(ClassModel.class);
classModels.add(classModel);
Log.i("Tag", "Msg");
}
Log.i("Tag", String.valueOf(classModels.size()));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
if (classModels==null){
Log.i("TAG","Null NO DATA IN DATABASE");
}
return classModels;
}
Asked
Active
Viewed 53 times
0

Doug Stevenson
- 297,357
- 32
- 422
- 441

M.U
- 39
- 9
-
Operation to firebase is *asynchronous*. so you have to wait to get data. You can use `LiveData` and observe it to get updated content. – Md. Asaduzzaman Dec 26 '19 at 18:17
-
sorry but I didn`t get it...because it worked when i implemented it in onCreate().. – M.U Dec 26 '19 at 18:26
-
Inside `onCreate` you implemented your logic inside `onDataChange `. Right? – Md. Asaduzzaman Dec 26 '19 at 18:34
1 Answers
1
Operation to firebase is asynchronous. so you have to wait to get data. You can use LiveData
and observe it to get updated content. Check below:
private MutableLiveData<ArrayList<ClassModel>> mutableClassModels = new MutableLiveData<>();
private ArrayList<ClassModel> classModels = new ArrayList<>();
public MutableLiveData<ArrayList<ClassModel>> getClassesList(){//upto to this every thing execute but from here the //execution jumps to if(condition) line below...and I get null arraylist in return
FbDb.child("Classes").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren())
{
ClassModel classModel = ds.getValue(ClassModel.class);
classModels.add(classModel);
Log.i("Tag", "Msg");
}
mutableClassModels.postValue(classModels);
Log.i("Tag", String.valueOf(classModels.size()));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return mutableClassModels;
}
And then observe it like below:
DbOnline.getClassesList().observe(this, new Observer<ArrayList<ClassModel>>() {
@Override
public void onChanged(ArrayList<ClassModel> classModels) {
// Do your operation here
}
});
Update:
Update your adapter like below:
//Initialize it, as it causing NullPointerException
ArrayList<ClassModel> clsList = new ArrayList<>();
public Adapter(Context context, String name) {
...
DbOnline=new Firebase_Database();
if (fragName.equals(listForClasses)) {
DbOnline.getClassesList().observe((LifecycleOwner) context, new Observer<ArrayList<ClassModel>>() {
@Override
public void onChanged(ArrayList<ClassModel> classModels) {
clsList =classModels;
clsList.size();
//Notify to refresh the items
notifyDataSetChanged();
}
});
} else {
sList = null;//DbOffline.getStudentsList("");
}
}

Md. Asaduzzaman
- 14,963
- 2
- 34
- 46
-
Check my updated answer. I have added the initialization part of classModels – Md. Asaduzzaman Dec 26 '19 at 18:49
-
-
-
Check my updated answer. 1. Initialize `clsList` and call `notifyDataSetChanged()` to refresh list – Md. Asaduzzaman Dec 26 '19 at 20:07
-
I am most grateful for your support. Thank you for taking the time to help me, I really do appreciate it. – M.U Dec 26 '19 at 20:22
-
No problem @Alix. You are most welcome and I am glad to help you. – Md. Asaduzzaman Dec 26 '19 at 20:25