I have a List<String> of names referring to Documents that I want to retrieve from FireStore. I want to access the contents after they are finished loading so I have implemented an OnCompleteListener in the Fragment that uses the data. However, I am not sure how to run a loop within a Task to query FireStore for each Document. I am querying FireStore in a Repository class that returns a Task object back through my ViewModel and finally to my Fragment. I want the Repository to return a Task so that I can attach an OnCompleteListener to it in order to know when I have finished loading my data.
My Repository Query method:
public Task<List<GroupBase>> getGroups(List<String> myGroupTags){
final List<GroupBase> myGroups = new ArrayList<>();
for(String groupTag : myGroupTags){
groupCollection.document(groupTag).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
myGroups.add(task.getResult().toObject(GroupBase.class));
}
}
});
}
return null; //Ignore this for now.
}
I know this won't return what I need but I am not sure how to structure a Task that incorporates a loop inside of it. Would I need to extract the contents of the List<String> in my Fragment and run individual queries for each item?
Any suggestions would be greatly appreciated.
According to your comment:
I have a List of Document names and I need to transform this to essentially a List of Tasks to retrieve the entire document.
To solve this, please use the following lines of code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("yourCollection");
List<String> myGroupTags = new ArrayList<>();
List<DocumentReference> listDocRef = new ArrayList<>();
for(String s : myGroupTags) {
DocumentReference docRef = collRef.document(s);
listDocRef.add(docRef);
}
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (DocumentReference documentReference : listDocRef) {
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get();
tasks.add(documentSnapshotTask);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
#Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list
for (Object object : list) {
GroupBase gb = ((DocumentSnapshot) object).toObject(GroupBase.class);
Log.d("TAG", tp.getPropertyname);
}
}
});
Related
I have set up a Firebase database that has 2 nodes. One node is called "ingredients" and the other is called "orders". I set up the nodes manually by using a JSON file and I just entered 1 entry in the node "orders" manually. You can see a screenshot of the structure of the database:
Now I would like to add further entries in the table "orders" (so basically I would like to add a further row).
In my Android application I use the following code:
// in the class definition of the Fragment
public DatabaseReference firebase_DB;
...
// in the oneCreate Method of the Fragment
firebase_DB = FirebaseDatabase.getInstance().getReference("orders");
...
// when clicking on a button in the Fragment which calls the onClick method
String id = firebase_DB.push().getKey();
DB_Object_Test currentOrder= new DB_Object_Test("testInfo_2", "testName", 2);
firebase_DB.child(id).setValue(currentOrder);
So basically the code runs without an error and the firebase_DB.child(id).setValue(currentOrder); is called. However, nothing changes in the Firebase console. I can't see any new entry there. The firebase database is properly connected to the app (at least Android Studio says that) and I have not specified any rules that would prohibit writing something on the database. The same data is stored without any problems in an SQLite database.
Can any one of you think about a possible reason for this problem? Why is the new row not written into the firebase database? I'll appreciate every comment.
EDIT: Here is the adjusted code that writes to the firebase database (by using 3 different approaches) after clicking a "Order" Button in the Fragment.
public void onClick(View view) {
if(view.getId() == R.id.ordering_button) {
/* Firebase approach 1
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("");
DatabaseReference usersRef = ref.child("orders");
Map<String, DB_Object_Test> order = new HashMap<>();
order.put("order_2", new DB_Object_Test("1", "testInfo_2", "testName", "2"));
usersRef.setValue(order);
*/
/*
Firebase approach 3 (by Alex)
*/
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = rootRef.child("orders");
Map<String, Object> order = new HashMap<>();
order.put("info", "No additional information");
order.put("name", "Another Test Order");
order.put("table", "15");
Log.e("LogTag", "Before write firebase");
ordersRef.child("order_2").setValue(order).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Log.e("LogTag", "During on Complete firebase");
if (task.isSuccessful()) {
Log.e("dbTAG", "Data successfully written.");
} else {
Log.e("dbTAG", task.getException().getMessage());
}
}
});
Log.e("LogTag", "Afer write firebase");
/* Firebase approach 2
DatabaseReference firebase_DB = FirebaseDatabase.getInstance().getReference("orders");
String id = firebase_DB.push().getKey();
DB_Object_Test currentOrder= new DB_Object_Test(id, "testInfo_2", "testName", "2");
firebase_DB.child(id).setValue(currentOrder);
//Option 2*
//firebase_DB.setValue(currentOrder);
Log.e("LogTag", "firebase_DB.child(id): " + firebase_DB.child(id));
*/
Navigation.findNavController(getView()).navigate(...);
}
}
To be able to add another object under your "orders" node, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = rootRef.child("orders");
Map<String, Object> order = new HashMap<>();
order.put("info", "No additional information");
order.put("name", "Another Test Order");
order.put("table", "15");
ordersRef.child("order_2").setValue(order).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "Data successfully written.");
} else {
Log.d("TAG", task.getException().getMessage());
}
}
});
This code will work only if you set the proper security rules in your Firebase console:
{
"rules": {
".read": true,
".write": true
}
}
Remember to keep these rules only for testing purposes. Besides that, you can also attach a complete listener to see if something goes wrong.
If you want to use DatabaseReference#push() method, then you should use the following line of code:
ordersRef.push().setValue(order);
Edit:
According to your last comment:
Database location: Belgium (europe-west1)
You should check my answer from the following post and add the correct URL to the getInstance() method:
getInstance() doesn't work with other location than us-central1 in Realtime Database
In your particular case it should be:
DatabaseReference rootRef = FirebaseDatabase.getInstance("https://drink-server-db-default-rtdb.europe-west1.firebasedatabase.app").getReference();
I have saved images in Firestore in form of an ArrayList of type String which has its URL. I want to get those images stored in a field "images" into an imageslider that has Slidemodel that takes ArrayList as a parameter.
The class slideModel has the following variables:
private String imageUrl;
private Integer imagePath;
private ScaleTypes scaleTypes;
private String title;
The code pasted below is iterating over documents not the fields of a particular document
db.collection("userimages").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult())
sliderDataArrayList.add(new SlideModel(document.getData().toString(), ScaleTypes.FIT));
imageSlider.setImageList(sliderDataArrayList,ScaleTypes.FIT);
}
}
The image slider takes ArrayList as a parameter and a Scale Type.
This code in the image slider is getting the documents into the slider not the field of that document that contains the images.Image of how the data is structured in firestore
I want to get the "field": "images" which has the ArrayList of strings containing image URLs and then store it in the sliderDataArrayList .
final List<SlideModel> sliderDataArrayList = new ArrayList<>();
PLEASE SUGGEST TO ME A BETTER WAY TO GET AROUND IT OR AN ANSWER TO THIS PROBLEM
THANK YOU!
According to your comment, because an answer for getting the data within the userimages array that exists in the NhVqAzmI6Cu9q8BTSMSr document, you say that it "will be really helpful", then here you are.
Because I cannot see any class that holds a List<String>, I will create one for you:
class DocumentClass {
public List<String> images;
}
Now to get the desired array, we need to create a reference that points to the NhVqAzmI6Cu9q8BTSMSr document:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference userImagesRef = rootRef.collection("userimages");
DocumentReference docRef = userImagesRef.document("NhVqAzmI6Cu9q8BTSMSr");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
DocumentClass documentClass = document.toObject(DocumentClass.class);
List<String> images = documentClass.images;
imageSlider.setImageList(images, ScaleTypes.FIT);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
For more info, please check the following article:
How to map an array of objects from Cloud Firestore to a List of objects?
I'm fairly new and I'm not familiar with using recyclerview adapter. I have this method to to load the data into the recyclerview adapter. The data will be filtered using merged queries. However the app crashed and I got java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.google.firebase.firestore.QuerySnapshot. Does anyone know how to fix this?
private void loadData(){
if(modelArrayList.size()>0)
modelArrayList.clear();
storageReference = FirebaseStorage.getInstance().getReference();
Query query = collectionReference.whereArrayContains("pet", pettype);
Query query2 = collectionReference.whereArrayContains("service", "Pet Sitting");
Query query3 = collectionReference.whereArrayContains("petnum", petnumber);
Query query4 = collectionReference.whereArrayContains("region", reg);
Query query5 = collectionReference.whereArrayContains("subregion", subreg);
Task task = query.get();
Task task2 = query2.get();
Task task3 = query3.get();
Task task4 = query4.get();
Task task5 = query5.get();
Task combinedtask = Tasks.whenAllSuccess(task, task2, task3, task4, task5);
combinedtask.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot querySnapshot: task.getResult()){
model model = new model(querySnapshot.getString("firstname"),
querySnapshot.getString("lastname"),
querySnapshot.getString("description"),
querySnapshot.getString("userID"));
modelArrayList.add(model);
}
adapter = new My_recyclerview_adapter(Pet_sitting_result.this, modelArrayList);
recycler_view.setAdapter(adapter);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Pet_sitting_result.this, "Error", Toast.LENGTH_SHORT);
}
});
}
Since your combinedTask is a result of several other Tasks, it is going to yield an List objects as a result, not a QuerySnapshot. So, your success callback should declare that it accepts a List<Task<?>> instead of a Task<QuerySnapshot>. There will be one item the list for each task you passed to whenAllSuccess(). You will have to write code to pull the individual snapshots for each query out of the list and deal with them each separately as needed.
You might want to familiarize yourself more with the Tasks API that you're using here and also study the API documentation for Tasks.whenAll().
This question already has an answer here:
Wait until Firestore data is retrieved to launch an activity
(1 answer)
Closed 2 years ago.
I am trying to add all the IDs of documents from a collection "Tasks" in a list call list. There is no error message but the list keeps coming empty. To try to find the error, whether it was not connecting to firebase or if there was another error, I added a toast message to see this and it showed that the for loop was running and it was retrieving the data but the list was still empty. I can assure you there is nothing wrong with the adapter or the recycler view. I am able to write to the firebase. Here is my code
CollectionReference collectionReference = db.collection("Tasks");
collectionReference
//.whereEqualTo("capital", true)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String s = document.getId();
list.add(s);
Toast.makeText(TaskList.this, document.getId(), Toast.LENGTH_SHORT).show();
}
} else {
}
}
});
mAdapter = new MyAdapter(list,this);
recyclerView.setAdapter(mAdapter);
Any help will be appreciated
Firebase sends asynchronous calls. Android does not have built-in functionality like await or something like that. So you have to set the data inside call success msg. If you try to read the data outside the call it won't work.
CollectionReference collectionReference = db.collection("Tasks");
collectionReference
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String s = document.getId();
list.add(s);
Toast.makeText(TaskList.this, document.getId(),Toast.LENGTH_SHORT).show();
}
mAdapter = new MyAdapter(list,this);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
} else {
}
}
});
I think you can easily use Lambda expressions for a purpose you are looking for, as an example you can do something like this:
List<String> MovieNameList = new ArrayList<>();
List<MovieDTO> movieDTOList = new ArrayList<>();
movieDTOList = getSearchResultFromAPI(searchPhrase);
MovieNameList = movieDTOList.stream()
.filter(x -> x.getName().contains(searchPhrase))
.map(MovieDTO::getName) // == .map(x -> x.getName)
.collect(Collectors.toList());
I hope it helps you!
I have a collection of users and I want to query all users from the database and display them in a RecyclerView except one, mine. This is my db schema:
users [collection]
- uid [document]
- uid: "fR5bih7SysccRu2Gu9990TeSSyg2"
- username: "John"
- age: 22
- //other users
How to query the database like so:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query q = db.collection("users").whereNotEqualTo("uid", uid);
So I need this query object to be passed to a FirestoreRecyclerOptions object in order to display all the other users in RecyclerView.
Is this even possible? If not, how can I solve this? Thanks!
Edit:
options = new FirestoreRecyclerOptions.Builder<UserModel>()
.setQuery(query, new SnapshotParser<UserModel>() {
#NonNull
#Override
public UserModel parseSnapshot(#NonNull DocumentSnapshot snapshot) {
UserModel userModel = documentSnapshot.toObject(UserModel.class);
if (!userModel.getUid().equals(uid)) {
return userModel;
} else {
return new UserModel();
}
}
}).build();
After days and days of struggling with this issue, I finally found the answer. I could not solve this without the help of #Raj. Thank you so much #Raj for the patience and guidance.
First off all, according to the answer provided by #Frank van Puffelen in his answer from this post, I stopped searching for a solution that can help me pass two queries to a single adapter.
In this question, all that I wanted to achieve was to query the database to get all the users except one, me. So because we cannot combine two queries into a single instance, I found that we can combine the result of both queries. So I have created two queries:
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query firstQuery = db.collection("users").whereLessThan("uid", uid);
Query secondQuery = db.collection("users").whereGreaterThan("uid", uid);
I'm having a UserModel (POJO) class for my user object. I found not one, but two ways to solve the problem. The first one would be to query the database to get all user objects that correspond to the first criteria and add them to a list. After that, query the database again and get the other user objects that correspond to the second criteria and add them to the same list. Now I have a list that contains all the users that I need but one, the one with that particular id from the queries. This is the code for future visitors:
firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
List<UserModel> list = new ArrayList<>();
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
UserModel userModel = document.toObject(UserModel.class);
list.add(userModel);
}
secondQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
UserModel userModel = document.toObject(UserModel.class);
list.add(userModel);
}
//Use the list of users
}
}
});
}
}
});
The second approach would be much shorter because I use Tasks.whenAllSuccess() like this:
Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();
Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
#Override
public void onSuccess(List<Object> list) {
//This is the list that I wanted
}
});
According to the official firestore documentation:-
Cloud Firestore does not support the following type of query:
Queries with a != clause. In this case, you should split the query
into a greater-than query and a less-than query. For example, although
the query clause where("age", "!=", "30") is not supported, you can
get the same result set by combining two queries, one with the clause
where("age", "<", "30") and one with the clause where("age", ">", 30).
If you are using FirestoreRecyclerAdapter then FirestoreRecyclerOptions will directly accepts the query using setQuery() method and hence not allows you to perform client side filtering.
If you try to apply filters in onBindViewHolder() while setting the data that might results in empty items in the recycler view. In order to resolve that refer Method 2.
So, the possible solution to your problem would be to create an integer field in your users collection under every document. Eg:-
users [collection]
- uid [document]
- uid: "fR5bih7SysccRu2Gu9990TeSSyg2"
- username: "John"
- age: 22
- check: 100
In this I have created a 'check' variable whose value is 100. So, put value of 'check' in all other documents as less than 100.
Now, you can easily make a query that finds documents with check<100 as:-
Query q = db.collection("users").whereLessThan("check", 100);
This will retrieve all your documents except the one you don't want. And while setting the data you can set other parameters skipping the check variable.
Method 2 (Client Side Filtering)
We can apply a check in onBindViewHolder() method that if the retrieved uid matches with current user uid then set the height of Recycler view as 0dp. As:-
ViewUserAdapter.java
public class ViewUserAdapter extends FirestoreRecyclerAdapter<User, ViewUserAdapter.ViewUserHolder>
{
String uid;
FirebaseAuth auth;
public ViewUserAdapter(#NonNull FirestoreRecyclerOptions<User> options)
{
super(options);
auth = FirebaseAuth.getInstance();
uid = auth.getCurrentUser().getUid();
}
#Override
protected void onBindViewHolder(#NonNull ViewUserHolder holder, int position, #NonNull User model)
{
DocumentSnapshot snapshot = getSnapshots().getSnapshot(position);
String id = snapshot.getId();
if(uid.equals(id))
{
RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)holder.itemView.getLayoutParams();
param.height = 0;
param.width = LinearLayout.LayoutParams.MATCH_PARENT;
holder.itemView.setVisibility(View.VISIBLE);
}
else
{
holder.tvName.setText(model.name);
holder.tvEmail.setText(model.email);
holder.tvAge.setText(String.valueOf(model.age));
}
}
}
2021 Update: This Is Supported
Howdy devs. It looks like this is now supported with the where operator used like this: citiesRef.where("capital", "!=", false);
Firestore doesn't support not equal to operation. So you need to filter the data at the client side. Since in you case you only have one extra item you can filter it out.
For that you may need to build your own recycler implementation where when adding data to recycler adapter data layer, you restrict the data when ever it matches your != condition.
I haven't explored recycler implementation firebase provided so I cannot say it supports data manipulation to adapter data or not.
Here is a good resource to start implementing recycler view : https://www.androidhive.info/2016/01/android-working-with-recycler-view/
The simplest solution would be to use a PagedListAdapter and create a custom DataSource for the Firestore queries. In the DataSource the Query can be transformed into an Array or ArrayList in which you can easily remove your item before adding the data to the method callback.onResult(...).
I used a similar solution to process data after a Firestore query in order to filter and sort by a time attribute, and then re-sort by a quality score attribute in the client before passing the data back in to callback.onResult(...).
Documentation
Google: Build your own data sources
Codepath: Paging Library Guide
Data Source Sample
class ContentFeedDataSource() : ItemKeyedDataSource<Date, Content>() {
override fun loadBefore(params: LoadParams<Date>, callback: LoadCallback<Content>) {}
override fun getKey(item: Content) = item.timestamp
override fun loadInitial(params: LoadInitialParams<Date>, callback: LoadInitialCallback<Content>) {
FirestoreCollections.contentCollection
.collection(FirestoreCollections.ALL_COLLECTION)
.orderBy(Constants.TIMESTAMP, Query.Direction.DESCENDING)
.whereGreaterThanOrEqualTo(Constants.TIMESTAMP, DateAndTime.getTimeframe(WEEK))
.limit(params.requestedLoadSize.toLong())
.get().addOnCompleteListener {
val items = arrayListOf<Content?>()
for (document in it.result.documents) {
val content = document.toObject(Content::class.java)
items.add(content)
}
callback.onResult(items.sortedByDescending { it?.qualityScore })
}
}
override fun loadAfter(params: LoadParams<Date>, callback: LoadCallback<Content>) {
FirestoreCollections.contentCollection
.collection(FirestoreCollections.ALL_COLLECTION)
.orderBy(Constants.TIMESTAMP, Query.Direction.DESCENDING)
.startAt(params.key)
.whereGreaterThanOrEqualTo(Constants.TIMESTAMP, DateAndTime.getTimeframe(WEEK))
.limit(params.requestedLoadSize.toLong())
.get().addOnCompleteListener {
val items = arrayListOf<Content?>()
for (document in it.result.documents) {
val content = document.toObject(Content::class.java)
items.add(content)
}
val sortedByQualityScore = ArrayList(items.sortedByDescending { it?.qualityScore })
callback.onResult(sortedByQualityScore)
sortedByQualityScore.clear()
}
}
}
Simpler and earlier client-side filtering (when you add items to your list):
Get the current user's ID by using Firestore's standard method.
Get the name of the doc for all the users in your user collection.
Before adding the user to
your RecyclerView list, check that the user it is about to add to your list is not the current user.
When done is this way, you can use the "not equals" method on the client side and not get into any Firestore issues. Another benefit is that you don't have to mess with your adapter or hide the view from a list-item you didn't want in the recycler.
public void getUsers(final ArrayList<Users> usersArrayList, final Adapter adapter) {
CollectionReference usersCollectionRef = db.collection("users");
Query query = usersCollectionRef
.whereEqualTo("is_onboarded", true);
query.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
final String otherUserID = document.getId();
FirebaseUser user = mAuth.getCurrentUser();
String currentUserID = user.getUid();
if (!otherUserID.equals(currentUserId)) {
usersArrayList.add(new User(otherUserID));
adapter.notifyDataSetChanged(); //Ensures users are visible immediately
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
You don't have to do all this
Just do normal query and hide the layout by setting getLayoutParams().height and width to 0 respectively. See example below.
if(firebaseUserId.equalIgnoreCase("your_holder_firebase_user_id"){
holder.mainLayout.setVisibility(View.INVISIBLE);
holder.mainLayout.getLayoutParams().height = 0;
holder.mainLayout.getLayoutParams().width = 0;
}else {
//show your list as normal
}
//This will hide any document snapshot u don't need, it will be there but hidden
here's my solution with flutter for usernames
Future<bool> checkIfUsernameExistsExcludingCurrentUid(
// TODO NOT DONE
String username,
String uid) async {
print("searching db for: $username EXCLUDING SELF");
bool exists = true;
QuerySnapshot result = await _firestore
.collection(USERS_COLLECTION)
.where(
"username",
isEqualTo: username,
)
.getDocuments();
List _documents = result.documents;
_documents.forEach((element) {
if (element['uid'] == uid) {
exists = false;
} else {
return true;
}
});
return exists;
}