I want to convert my document snapshots to a pojo object. but I can't get the logic behind this .toObject() method. can someone explain it's working, i.e. arguments, how it works, how i should use it in firebase.
private ArrayList<POJO> mArrayList = new ArrayList<>();
public ArrayList<POJO> getmArrayList() {
return mArrayList;
}
public void getListItems() {
db.collection(collection_name).get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if (documentSnapshots.isEmpty()) {
Log.d(TAG, "onSuccess: LIST EMPTY");
return;
} else {
// Convert the whole Query Snapshot to a list
// of objects directly! No need to fetch each
// document.
List<POJO> types = documentSnapshots.toObjects(POJO.class);
// Add all to your list
mArrayList.addAll(types);
Log.d(TAG, "onSuccess: " + mArrayList);
}
}
});
}
my MainActivity file is this -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FireStore mido= new FireStore();
ArrayList<POJO> mArr = mido.getmArrayList();
List<POJO> types = new ArrayList<>();
mido.getListItems();
types.addAll(mArr);
String[] iranoutofnames= new String[2];
int i = 0;
for (POJO hello: types) {
iranoutofnames[i] = hello.getName();
i++;
}
for (int j = 0; j < 2; j++) {
Toast.makeText(this, iranoutofnames[0], Toast.LENGTH_LONG).show();
Toast.makeText(this, iranoutofnames[1], Toast.LENGTH_LONG).show();
}