Issue
I would like to create an application able to receive the id of a document on Firestore with all its fields and put them in a RecyclerView Adapter. For now, I can only get the fields, but I don't understand how to get the document ID. This is the code:
public class TestPage extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<User> userArrayList;
MyAdapter myAdapter;
FirebaseFirestore db;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_page);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Fetching Data...");
progressDialog.show();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
db = FirebaseFirestore.getInstance();
userArrayList = new ArrayList<User>();
myAdapter = new MyAdapter(TestPage.this,userArrayList);
recyclerView.setAdapter(myAdapter);
EventChangeListener();
}
private void EventChangeListener() {
db.collection("Attesa").orderBy("firstName", Query.Direction.ASCENDING)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if(error != null){
if(progressDialog.isShowing())
progressDialog.dismiss();
return;
}
for(DocumentChange dc : value.getDocumentChanges()){
if(dc.getType() == DocumentChange.Type.ADDED){
userArrayList.add(dc.getDocument().toObject(User.class));
}
myAdapter.notifyDataSetChanged();
if(progressDialog.isShowing())
progressDialog.dismiss();
}
}
});
}
Class User:
public class User {
String firstName, lastName, age;
public User(){}
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Class MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context context;
ArrayList<User> userArrayList;
public MyAdapter(Context context, ArrayList<User> userArrayList) {
this.context = context;
this.userArrayList = userArrayList;
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder((v));
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
User user = userArrayList.get(position);
holder.firstName.setText(user.firstName);
holder.lastName.setText(user.lastName);
}
@Override
public int getItemCount() {
return userArrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView firstName, lastName;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
firstName = itemView.findViewById(R.id.tvFirstName);
lastName = itemView.findViewById(R.id.tvLastName);
}
}
}
How can I take the ID of the document in addition to firstName and lastName? so in this case the number?
Solution
While as Alex says it is indeed to store the document ID in the data of that document, this is not required.
You can also use a DocumentId
annotation on the POJO instead, and then let Firestore automatically read it from the database. As the documentation of the DocumentId
annotation says:
Annotation used to mark a POJO property to be automatically populated with the document's ID when the POJO is created from a Cloud Firestore document (for example, via
toObject(Class)
).
So something like:
public class User {
String firstName, lastName, age, docId;
public User(){}
public User(String firstName, String lastName, String docId) {
this.firstName = firstName;
this.lastName = lastName;
this.docId = docId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@DocumentId
public String getDocId() {
return docId;
}
@DocumentId
public void setDocId(String docId) {
this.docId = docId;
}
}
Also see:
* https://stackoverflow.com/questions/49865690/firestore-how-to-properly-store-the-document-id-in-the-model-class
Answered By - Frank van Puffelen
Answer Checked By - David Goodson (JavaFixing Volunteer)