Basically I have to listen a object. Please see the attached images.
Memo List
Memo Object
So I need to get all memos which equal to hash_tag = "love". How to overcome this problem?
Query query = memo.child(Memo.HASH_TAG).startAt("love");
query.addChildEventListener(mMemoChildEventListener);
You can check some basics guides for retrieving data here https://firebase.google.com/docs/database/admin/retrieve-data
Android - How to query in firebase
MemoObject memoobject=new MemoObject();
ArrayList<MemoObject> memoobjectArrayList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(Memo).orderByChild("hash_tag").equalTo("love");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot dataSnapshots :dataSnapshot.getChildren()) {
memoobject=dataSnapshots.getValue(MemoObject.class);
for(int i=0;i<memoobject.getHashTag().size();i++){
if(memoobject.getHashTag().get(i).equals("love"){
memoobjectArrayList.add(memoobject);
}
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Related
I have developed an admin side application from where I will be adding the email id, pin, city and country data to the firebase realtime database. Then I will be providing the email and pin to the customer. So my simple target is that when the customer logins, the data of city and country should be displayed on his application. To do this I have to search the database for that particular customer's node which can be done through the email id which he puts during login.
I tried my best but everytime null is returned. Please have a look at my code and help me out.
void searchQuery(String email){
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("arvi-admin").orderByChild("mailId").equalTo(email);
query.addValueEventListener(valueEventListener);
}
ValueEventListener valueEventListener = new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
try {
DataModel obj = dataSnapshot.getValue(DataModel.class);
logData(""+obj.getCity()); //the values are null
}catch (Exception e){
Log.d("##","Error");
e.printStackTrace();
}
}
#Override
public void onCancelled(DatabaseError databaseError){
}
};
Firebase Database Image
To solve this, please change the following line of code:
Query query = mFirebaseDatabaseReference.child("arvi-admin").orderByChild("mailId").equalTo(email);
to
Query query = mFirebaseDatabaseReference.orderByChild("mailId").equalTo(email);
As you can see, I have removed the call to .child("arvi-admin") because the root is not a child and should not be added in your reference.
Edit:
Query query = mFirebaseDatabaseReference.orderByChild("mailId").equalTo(email);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
DataModel obj = ds.getValue(DataModel.class);
Log.d(TAG, obj.getCity());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
Hello, I am new to Android development, so I'm hoping for your understanding. I would like to ask how I can retrieve all data under 'values' and get the sum. As you can see, the 'keys' are random-generated keys, which were sent from an Arduino device to Firebase, but the values being sent to Firebase are correct. I want to get the sum and display it on an Android app in a single TextView. I am using Android Studio as IDE for my Android app.
I've tried other snippets from Stack Overflow as well, but it's too confusing for my level of knowledge in Android and I can't make it work. Thank you.
Try like this, may be this will help to achieve your task.
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
ref.child("data/values").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int toalSum=0;
if (dataSnapshot.getValue() != null) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//depending upon what datatype youre using caste to it.
totalSum = totalSum+(int)snapshot.getValue();
}
Log.d("LOG","Toatal sum-"+totalSum)
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this dataSnapshot.getChildrenCount() will return size
DatabaseReference dbref=FirebaseDatabase.getInstance().getReference();
dbref.child("url/values")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int size = dataSnapshot.getChildrenCount(); //Here u can see the count
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
To count those values, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference valuesRef = rootRef.child("data").child("values");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int sum = o;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
int value = ds.getValue(Integer.class);
sum = sum + value;
}
Log.d("TAG", sum);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
valuesRef.addListenerForSingleValueEvent(eventListener);
Your outout will be:
1780
try this basic split string.
database.geteDatabase().getReference().child("data").child("values")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//dataSnapshot.getValue().toString() = {a=1,b=2,c=3}
String[] value = dataSnapshot.getValue().toString().split("[{,}]");
String[] getA= value[1].split("="); //value[1]='a=1'
String[] getB= value[2].split("="); //value[2]='b=2'
String[] getC= value[3].split("="); //value[3]='c=3'
//what you want to do here.
}
#Override
public void onCancelled(DatabaseError databaseError) { }
});
this can use for other get basic multi value from firebase database.
I am trying to sort my Firebase DB data on the Android Studio side and apply it in real-time. My codes successfully updates the view immediately when I make a manual change on my DB, but I wonder if I can sort by name using the code. I thought orderByChild was the function to do that, but it does not change the database.
Function that fetches data from firebase and displays:
private void fetchRepoData(String profileUserID) {
profileUserDatabase = repoReference.child(profileUserID);
//DatabaseReference reposReference = profileUserDatabase.child("Repos");
profileUserDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
repoListItems.clear();
repoNameList.clear();
userNameList.clear();
descriptionList.clear();
Toast.makeText(getActivity(), "changed", Toast.LENGTH_SHORT).show();
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
repoNameList.add(snapshot.getValue(RepoInfo.class).getRepoName());
userNameList.add(snapshot.getValue(RepoInfo.class).getOwner());
descriptionList.add(snapshot.getValue(RepoInfo.class).getDescription());
}
for(int i = 0; i < repoNameList.size(); i++) {
repoListItems.add(new RepositoryItem(i, repoNameList.get(i), userNameList.get(i), descriptionList.get(i)));
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Radio button for sorting by name
sortByNameRadio.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
repoReference.child("subchild").orderByChild("name");
adapter.notifyDataSetChanged();
}
}
);
When you call orderByChild() it returns a new Query object. You'll need to store a reference and attach a listener that query object.
profileUserDatabase = repoReference.child(profileUserID);
Query query = repoReference.child("subchild").orderByChild("name");
query.addValueEventListener(new ValueEventListener() {
for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
System.out.println(snapshot.getKey());
...
I have a problem regarding querying the firebase database using a value and getting the specific node. My schema is shown here:
In my schema 'workLocations' belongs to an 'excavationWorks', and 'excavationWorks' belongs to an 'excavationLists'.
That means that the path to a specific workLocation is excavationLists/excavationWorks/workLocations/(specific workLocation)
The problem that I have is that I want to query the workLocations node by the location value (let's say London) and get the parent node (the red circled key which is the key of the specific workLocation).
I have search and read many posts but I haven't managed to make it work.
My code looks like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("workLocations").orderByChild("location").equalTo("London");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot workLoc : dataSnapshot.getChildren()) {
// do something with the individual "issues"
Log.d(TAG, workLoc.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thank you
To achieve this, you need to query your database twice like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference workLocationsRef = reference
.child("excavationLists")
.child("excavationWorks")
.child("workLocations");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String key = ds.getKey();
Query query = workLocationsRef.child(key).orderByChild("location").equalTo("London");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
String description = snapshot.child("description").getValue(String.class);
Log.d("description", description);
String partentKey = snapshot.getRef().getParent().getKey();
Log.d("partentKey", partentKey);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
workLocationsRef.addListenerForSingleValueEvent(valueEventListener);
I want to get item in the last node added in firebase database from my Android. You can see on the image below i'm not sure how to get the specific node, because unique key is created by Firebase. How to reference to auto-created node and child inside? Thanks a lot
The last node
Try this:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query lastQuery = databaseReference.child("mp").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String message = dataSnapshot.child("message").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Handle possible errors.
}
});
Hope this helps!
This might work:
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("mp");
Query query = db.orderByKey().limitToLast(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
Log.d("User key", child.getKey());
Log.d("User val", child.child("message").getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: Handle errors.
}
});
I prefer to write and retrieve data through objects.
MyObject is POJO.
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
MyObject myObject = data.getValue(MyObject.class);
Log.i(TAG, data.getKey() + " = " + myObject.toString());
}
}
console.log('last messages', messages[messages.length-1]);