ANDROID: How to iterate data in firebase when adding a new data? - android

I have a project that involves adding data to Firebase and every time I add a new data I want to see the result of iterating the child.
Like for example:
I have 10 data under wordbank and I want the new added data to be inserted at the end of the list. How do you solve this? Please help! Thank you very much!

Related

Failing to Update Array in Firestore

I am trying to update Array in Firestore. From what I know you cant just append to the array in Firestore instead you need to replace the complete array with a new one.
Following is the code in Kotlin where I was trying to update the array:
holder.button.setOnClickListener {
pending.add(o[0])
Firebase.firestore.collection("profiles").document("YHMauLouORRtrYBV2h4dHJ5A0s72").update(
"Pending" , "$pending"
)
Here o[0] is the string I desire to Append to the already Existing Array named pending
Initialy in Firestore :-
Output I am getting :-
Output I desire:-
Basically While updating its converting the Array to a sting of list and pushing to Firestore. How can I get the Desired Output as shown in image ?
Thanks in Advance
That's not how to update an array in Firestore. To achieve that, you should use:
Firebase.firestore.collection("profiles")
.document("YHMauLouORRtrYBV2h4dHJ5A0s72")
.update("Pending", FieldValue.arrayUnion("Banana"))
When you're calling update without the second argument it means that you want to update a regular field.

Cloud Function to sort the Firebase Child from inner child

I need to have one problem sorted out. I want a cloud function, where I need to loop through every Posts that has uniqueID and I want to sort those uniqueID's according to the value of TotalReactions. To me more vivid, suppose, the first post has totalReactions = 5, and second one has totalReactions = 6, then I need to have another RealTimeDatabase "PostArranged" which sorts them in descending order, so second post which has Total Reaction would be in first and then follows the one which has lesser TotalReaction...
Here is how my firebase realtime database looks like:
Posts
-L29TD-nsUYRu3wYcCQl
Caption: "First Screenshot"
CurrentUserReaction: "notreacted"
Image: "https://firebasestorage.googleapis.com/v0/b/nep..."
ReactingUser
<user-id>:"1"
<user-id>:"2"
Time: "13:40:54"
TotalReactions: "2"
Unique: "-L29TD-nsUYRu3wYcCQl"
UserPhoto: "https://firebasestorage.googleapis.com/v0/b/nep..."
Username: "Me"
Any help would be appreciated ..
This is not a good way to do what you want to do. In this method the database has to be sorted to another "ArrangedDatabase" every time a reaction is added which will happen in a high frequency. So it will be very expensive to do this with Cloud Functions.
Instead use orderByChild(), orderByKey(), orderByValue() to sort data when you're querying data.
Sorting data - Firebase documentation this show it quite well.
For anyone who want to do this in your RecyclerViewAdapter provided by Firebase, it is easily done... When you pass in those 4 parameters. Do not pass the database Reference, instead, make a new query, and make sure to sort that thing either by OrderByChild(), OrderByValue() or anything you want to order by... i.e.
In my case, I will do something like this:
q = mDatabaseReference.orderByChild("TotalReactions");
and pass q as parameter inside FirebaseAdapter, like this:
FirebaseRecyclerAdapter<Posts,PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostViewHolder>(
Posts.class, R.layout.each_post_layout , PostViewHolder.class, q
) {
.
}

How to add a specific key with a specific value in a firebase database with Android?

I have a realtive simple Firebase database, in which i have 2 models. A ListModel and a UserModel. In my Lists, i'm using push() method to generate unique ids. Each unique id i want to be added as a key and "true" as it's value under Users/gmail#gmail,com/Lists.
When i add the first list, the database looks like this:
And everything works fine, but when i try to add another one, the database looks like this:
In Users/gmail#gmail,com/Lists, the first one is overwritten by the second insert. How can i add the specific id and the specific value, as a new item as shown below?
And this is my code:
final UserModel um = new UserModel();
um.setUserEmail("gmail#gmail,com");
userDatabaseReference.setValue(um);
ListModel lm = new ListModel();
lm.setListName(listName);
listKeyDatabaseReference = listDatabaseReference.push();
listKey = listKeyDatabaseReference.getKey();
listKeyDatabaseReference.setValue(lm);
listDatabaseReference.child(listKey).child("Users").child("gmail#gmail,com").setValue("true");
userDatabaseReference.child("gmail#gmail,com").child("Lists").child(listKey).setValue("true");
Thanks in advance!
Check the official doc:
For basic write operations, you can use setValue() to save data to a specified reference, replacing any existing data at that path.
Your problem is here:
userDatabaseReference.setValue(um);
In this way you are overriding all children in the userDatabaseReference path.
It means that the first record in Users/gmail#gmail,com/Lists is just deleted when you are adding the second one.
Before using the
userDatabaseReference.setValue(um);
you can check if the record exists.
If doesn't exist use the setValue to add the user-model with all its data.
If it exists, just skip this step and add the data in the lists path inside the same user.

Implementing OR Condition using Parse Android SDK?

I am new to Parse and i am working on a Project that uses Android PARSE Sdk so i was wondering on how can i make a where query with or condition using the android sdk.
I want to make a query like this [Psuedo code]
Select * from employ where employId is ("1","2","3")
I found this on parse documentation, i don't know if it helps or not.
Edit:
This is what i found on PARSE but its not working
String[] id= {"1","2","3"};
query.whereContainedIn("employId ", Arrays.asList(id));
It returns me an empty list but if i query them one by one i get result... Can anyone tell me whats wrong ?
You can use whereContainedIn to select specific rows. See this post you can get more ideas. https://www.parse.com/questions/different-arrays-and-wherecontainedin-for-android.
List<String> employId = new ArrayList<String>();
employId.add("1"); employId.add("2"); employId.add("2");
query.whereContainedIn("employId", employId);
If you are still not clear. check this https://www.parse.com/docs/android_guide#queries
I have found the solution and i must say its pretty lame of PARSE to not mention this anywhere in there documentation.
The problem was that the values that i was using inwhereContainedIn method were of type String but in reality they were pointers to another table's row.
I was trying to get the values using only there ids[as it is displayed on parse] but instead i had to pass the whole object in order to retrieve them. That was the reason on why it was returning empty list.
The thing is Even though it displays IDs [pointer to object in a table] we cant search using only ID's instead we have to use complete Parse objects if we want to search a table based on Specific Object.

How I can show my thousands of records in ScrollView

I am new to Android.I have stuck on one place,
I am fetching data from server that have thousand of records.I am using Limit 0,50 in my Query.SO it is showing only 50 records,But if I don't use Limit My application goes to crash.What should I do to show all my data in a List form.Is there any concept of Paging in Android or any other way.
Please suggest me the way.or links If anybody know it.
Thanks
save data fetched from server into db, using cursor to read data from db or set cache
Have you tried to use a customized list adapter ? In the getView method, use a combination of convertView + view holder, as explained here : http://chrislee.kr/wp/2011/01/01/making-the-android-ui-fast-and-efficient-by-romain-guy/

Categories

Resources