Reputation: 95
I want to read each Object list from the document "list_of_clients" and export it to a custom List that i created
In Realtime Database there is a function called GenericTypeIndicator which helps you getting the values, this is similar to what i want but on Cloud FireStore there is no such function
I'm very new to android and i don't know how much more i can explain this but here are some pics:
This is my user_list.java
private String user_addr;
private String user_name;
private String user_phone;
public user_list(String user_addr, String user_name, String user_phone) {
this.user_addr = user_addr;
this.user_name = user_name;
this.user_phone = user_phone;
}
public user_list() {
}
public String getUser_phone() {
return user_phone;
}
public void setUser_phone(String user_phone) {
this.user_phone = user_phone;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_addr() {
return user_addr;
}
public void setUser_addr(String user_addr) {
this.user_addr = user_addr;
}
What i tried:
db.collection("clients").document("list_of_clients")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
//if read successful
DocumentSnapshot document = task.getResult();
List<user_list> messages = document.getData();
Toast.makeText(getApplicationContext(), document.getId() + " => " + document.getData(), Toast.LENGTH_LONG).show();
} else {
//eroare
}
}
});
Upvotes: 1
Views: 6515
Reputation: 138824
To make it work, please see the following steps. So, in order to deserialize the fields correctly, please consider using a model class that looks like this:
public class UserList {
private String userAddr, userName, userPhone;
public UserList() {}
public UserList(String userAddr, String userName, String userPhone) {
this.userAddr = userAddr;
this.userName = userName;
this.userPhone = userPhone;
}
public String getUserAddr() {return userAddr;}
public String getUserName() {return userName;}
public String getUserPhone() {return userPhone;}
}
See the naming convention? Firebase will always look after fields that look like userAddr
and a public getter that look like getUserAddr()
.
To add a UserList
object in a correct way to your Cloud Firestore database, please use the following code:
UserList userList = new UserList("Bolintin Vale", "Bogdan", "0251206260");
String docId = rootRef.collection("clients").document().getId();
rootRef.collection("clients").document(docId).set(userList);
To actually read the data, please use the following code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("clients").document("pu8NKFPNUKYgcy0yOitW").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserList userList = documentSnapshot.toObject(UserList.class);
Log.d(TAG, userList.getUserName());
}
});
As you can see, I have used in my code the pu8NKFPNUKYgcy0yOitW
id that was generated before. The output will be:
Bogdan
Bafta! ;)
Upvotes: 1