Retrieving data in alphabetical order using Firebase in Android Studio [duplicate] - android

This question already has answers here:
How can I sort an ArrayList of Strings in Java?
(5 answers)
Closed 7 years ago.
This question is a follow up question from a previous post.
Is there anyway to order the results alphabetically? I'm looking for something like Query's orderByValue().
...
protected ArrayList<String> usernames = new ArrayList<>();
Firebase userRef = ref.child("users");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
usernames.clear(); // Clear list before updating
for (DataSnapshot child : dataSnapshot.getChildren()) {
String username = (String) child.child("username").getValue();
usernames.add(username);
Log.v(TAG, usernames + "");
}
...

I believe you want Collections.sort(usernames) once you've read all of the children from the dataSnapshot.
How can I sort an ArrayList of Strings in Java?

Related

How to get data from fields, not entire document. Firestore [duplicate]

This question already has answers here:
How to access a specific field from Cloud FireStore Firebase in Swift
(5 answers)
Closed 3 years ago.
I am trying to get data from fields on my firebase firestore, but not the whole document.
Fields that i want
Here is my code that get the whole document.
db.collection("Reminder").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (!queryDocumentSnapshots.isEmpty()) {
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot d : list) {
dbReminder p = d.toObject(dbReminder.class);
p.setId(d.getId());
remtasklist.add(p);
}
adapter.notifyDataSetChanged();
}
}
});
Thanks in advance.
Edit: I updated with the collection pic.
Collection
You can get specific fields from the documentSnapshot.
String location = documentSnapshot.getString("inLocation");
String time = documentSnapshot.getString("time");//assuming time here is string
String title = documentSnapshot.getString("title");

Android ignore Case sensitive with Firebase whereEqualTo [duplicate]

This question already has answers here:
Cloud Firestore Case Insensitive Sorting Using Query
(3 answers)
Closed 4 years ago.
db.collection("UserInformation").whereEqualTo("userName" , searchEditText.getText().toString())
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(final QuerySnapshot documentSnapshots) {
if (documentSnapshots.isEmpty())
{
}else
{
List<UserModel> listUsers = documentSnapshots.toObjects(UserModel.class);
final SearchUserListAdapter adapter = new SearchUserListAdapter(getBaseContext() , listUsers);
searchLista.setAdapter(adapter);
}
}
});
i need to ignore case sensitive and find the "nickname" however i typed the nickname with capital or small letters or both i tried to add startAt() or endAt() and i didn't worked for me and the all answers talk about to save the nickname with upper and lower case but it is not good logic to me
I think there is no support for lowercase searches in Firebase. An idea to handle this would be to store the lowercase string along side the original string and then query the lowercase string instead.

Parse unknown keys from Firebase database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Have a firebase database that is being updated by a remote device and another side that has to be listed, the data comes, but I do not know how could I use Firebase reflection to parse the list of objects as shown below
Assuming that oxygen_level is a direct child of Firebase root, to display those values, please use the code below:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference oxygenLevelRef = rootRef.child("oxygen_level");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String oxygen_mg = ds.child("oxygen_mg").getValue(String.class);
String sensor_status = ds.child("sensor_status").getValue(String.class);
String time = ds.child("time").getValue(String.class);
Log.d("TAG", oxygen_mg + " / " + sensor_status + " / " + time);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
oxygenLevelRef.addListenerForSingleValueEvent(eventListener);

How to apply multiple query in android firebase? [duplicate]

This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 6 years ago.
This is My Firebase database structure
i want to get data like
category = "cat" and level = "1"
Here is my code
Query query = firebaseReference.orderByChild("category").equalTo("cat1").limitToFirst(20);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
UTILS.Log.e(TAG, "List Of User Count " + dataSnapshot.getChildrenCount());
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
//FCreateUSer modelRegister = dataSnapshot1.getValue(FCreateUSer.class);
UTILS.Log.e(TAG, "User Email :" + dataSnapshot1.child("question").getValue());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
it's wrok but how to use two condition in firbase i try two orderByChild but it's give error java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!
Take a look at this video (it's in JavaScript, but it exactly solves your problem): https://youtu.be/sKFLI5FOOHs?t=612
Basically, you need to structure your data for your queries. In OP example Question will have field like category_level: "cat_1". Then you can perform query equalTo("cat_1"). You can leave out both category and level fields if you need it for other queries (but you will need to handle duplication in that case).
If you know that your number of items is small, simplest solution is to just pull all category=cat and filter level=1 items.

android firebase storage retrieve in listview [duplicate]

This question already has answers here:
How to get a list of all files in Cloud Storage in a Firebase app?
(20 answers)
Closed 6 years ago.
Suppose I have 5 audio files in Firebase Storage and I want to retrieve those 5 audio files and display them in a list view. As I click on any of 5 files then they should be downloaded to local phone memory?
Get the list of your 5 songs on your refernece.then add this list to adapter
your_ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
songslist= new ArrayList<String>();
Toast.makeText(DataFromFirebase.this, "number of songs are " + dataSnapshot.getChildrenCount(), Toast.LENGTH_LONG).show();
//String value = (String) dataSnapshot.getValue();
sizetemp=dataSnapshot.getChildrenCount();
// Result will be holded Here
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
songslist.add(String.valueOf(dsp.getKey())); //add result into array list
//NOW YOU HAVE ARRAYLIST WHICH HOLD RESULTS
}
#Override
public void onCancelled(DatabaseError firebaseError) {
}
});

Categories

Resources