0

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();
    }
  • what you want to try retrieve values according to pojo and pass in arraylist ? – Ashish Jul 25 '19 at 12:38
  • can u explain pojo in detail, i am really messed up –  Jul 25 '19 at 12:44
  • Pojo is a Data class which holds the getter and setter of values you received from Server. For More details [read this](https://stackoverflow.com/a/48034214/10182897) – Ashish Jul 25 '19 at 12:45
  • yeah, I actually made a java class named POJO in my app, and I am using .toObject(POJO.class) to convert documentsnapshots to my POJO class objects, but I don't get how this .toObject works, any documentation. and I am also reading data from firestore and storing as POJO object but when I use getter to get the data, Its empty. but I don't have empty values put in my cloud firestore database. –  Jul 25 '19 at 13:03
  • Please post your data response and database structure to understand the proper way and provide you pojo. – Ashish Jul 25 '19 at 13:04
  • hey ashish I updated it plz check, and don't mind, I really ran out of names to name my variable. –  Jul 25 '19 at 13:08
  • Please post your database structure image to understand your code properly. – Ashish Jul 25 '19 at 13:09
  • man, I don't know what that is(database structure image) –  Jul 25 '19 at 13:12
  • Please check [this](https://i.stack.imgur.com/fUvuA.png) – Ashish Jul 25 '19 at 13:13
  • I get, it but i have sensitive data, but I can assure you its not empty. –  Jul 25 '19 at 13:15
  • Take screenshot hide the important data and provide the fields name of your database structure. – Ashish Jul 25 '19 at 13:16
  • sorry, i will change it –  Jul 27 '19 at 04:26

1 Answers1

0

.toObject() is used to convert some data to some POJO data type.