Getting count before executing a method in android












1














Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("From").equalTo(MessageRecieverId);
mSeenRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String keyId = ds.getKey();



DatabaseReference mSendersRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(keyId);
DatabaseReference mRecieversRef = db.child("Messages").child(MessageRecieverId).child(MessageSenderId).child(keyId);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("Seen", true);
mSendersRef.updateChildren(childUpdates);
mRecieversRef.updateChildren(childUpdates);

}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});


This method turns a specific child called seen to true in my firebase database... I want to get the count of the number of childs which isnt true yet before turning into true... how can i achieve that... I want to execute a method just before turing it into true so i need the count



Database



enter image description here










share|improve this question
























  • Please add your database structure.
    – Alex Mamo
    Nov 20 at 14:25










  • done @AlexMamo......
    – user10643013
    Nov 20 at 14:55
















1














Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("From").equalTo(MessageRecieverId);
mSeenRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String keyId = ds.getKey();



DatabaseReference mSendersRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(keyId);
DatabaseReference mRecieversRef = db.child("Messages").child(MessageRecieverId).child(MessageSenderId).child(keyId);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("Seen", true);
mSendersRef.updateChildren(childUpdates);
mRecieversRef.updateChildren(childUpdates);

}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});


This method turns a specific child called seen to true in my firebase database... I want to get the count of the number of childs which isnt true yet before turning into true... how can i achieve that... I want to execute a method just before turing it into true so i need the count



Database



enter image description here










share|improve this question
























  • Please add your database structure.
    – Alex Mamo
    Nov 20 at 14:25










  • done @AlexMamo......
    – user10643013
    Nov 20 at 14:55














1












1








1







Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("From").equalTo(MessageRecieverId);
mSeenRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String keyId = ds.getKey();



DatabaseReference mSendersRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(keyId);
DatabaseReference mRecieversRef = db.child("Messages").child(MessageRecieverId).child(MessageSenderId).child(keyId);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("Seen", true);
mSendersRef.updateChildren(childUpdates);
mRecieversRef.updateChildren(childUpdates);

}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});


This method turns a specific child called seen to true in my firebase database... I want to get the count of the number of childs which isnt true yet before turning into true... how can i achieve that... I want to execute a method just before turing it into true so i need the count



Database



enter image description here










share|improve this question















Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("From").equalTo(MessageRecieverId);
mSeenRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String keyId = ds.getKey();



DatabaseReference mSendersRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(keyId);
DatabaseReference mRecieversRef = db.child("Messages").child(MessageRecieverId).child(MessageSenderId).child(keyId);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("Seen", true);
mSendersRef.updateChildren(childUpdates);
mRecieversRef.updateChildren(childUpdates);

}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});


This method turns a specific child called seen to true in my firebase database... I want to get the count of the number of childs which isnt true yet before turning into true... how can i achieve that... I want to execute a method just before turing it into true so i need the count



Database



enter image description here







java android database firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 15:02

























asked Nov 20 at 14:20







user10643013



















  • Please add your database structure.
    – Alex Mamo
    Nov 20 at 14:25










  • done @AlexMamo......
    – user10643013
    Nov 20 at 14:55


















  • Please add your database structure.
    – Alex Mamo
    Nov 20 at 14:25










  • done @AlexMamo......
    – user10643013
    Nov 20 at 14:55
















Please add your database structure.
– Alex Mamo
Nov 20 at 14:25




Please add your database structure.
– Alex Mamo
Nov 20 at 14:25












done @AlexMamo......
– user10643013
Nov 20 at 14:55




done @AlexMamo......
– user10643013
Nov 20 at 14:55












1 Answer
1






active

oldest

votes


















0














If you want to know how many items the value of your Seen property true, please use the following lines of code:



Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("Seen").equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int count = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
count = count + 1;
}
Log.d(TAG, String.valueOf(count)); //Will print the number of seen messages
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
mSeenRef.addListenerForSingleValueEvent(valueEventListener);


As you can see, you just simply need to count the number of occurrences.



Another approach would be to use dataSnapshot.getChildrenCount() which will return a value of type long, which actually represents the number of children that exist within the dataSnapshot object.






share|improve this answer





















  • But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
    – user10643013
    Nov 20 at 15:21










  • Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
    – Alex Mamo
    Nov 20 at 15:31












  • let me make sure... in ur query why is it Seen.equaTo true?
    – user10643013
    Nov 20 at 15:38










  • It's because that's what you've asked. Give it a try and tell me if it works
    – Alex Mamo
    Nov 20 at 15:54










  • Its still fetching all the data inside sendersid and recievers id
    – user10643013
    Nov 20 at 15:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395053%2fgetting-count-before-executing-a-method-in-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you want to know how many items the value of your Seen property true, please use the following lines of code:



Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("Seen").equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int count = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
count = count + 1;
}
Log.d(TAG, String.valueOf(count)); //Will print the number of seen messages
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
mSeenRef.addListenerForSingleValueEvent(valueEventListener);


As you can see, you just simply need to count the number of occurrences.



Another approach would be to use dataSnapshot.getChildrenCount() which will return a value of type long, which actually represents the number of children that exist within the dataSnapshot object.






share|improve this answer





















  • But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
    – user10643013
    Nov 20 at 15:21










  • Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
    – Alex Mamo
    Nov 20 at 15:31












  • let me make sure... in ur query why is it Seen.equaTo true?
    – user10643013
    Nov 20 at 15:38










  • It's because that's what you've asked. Give it a try and tell me if it works
    – Alex Mamo
    Nov 20 at 15:54










  • Its still fetching all the data inside sendersid and recievers id
    – user10643013
    Nov 20 at 15:55
















0














If you want to know how many items the value of your Seen property true, please use the following lines of code:



Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("Seen").equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int count = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
count = count + 1;
}
Log.d(TAG, String.valueOf(count)); //Will print the number of seen messages
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
mSeenRef.addListenerForSingleValueEvent(valueEventListener);


As you can see, you just simply need to count the number of occurrences.



Another approach would be to use dataSnapshot.getChildrenCount() which will return a value of type long, which actually represents the number of children that exist within the dataSnapshot object.






share|improve this answer





















  • But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
    – user10643013
    Nov 20 at 15:21










  • Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
    – Alex Mamo
    Nov 20 at 15:31












  • let me make sure... in ur query why is it Seen.equaTo true?
    – user10643013
    Nov 20 at 15:38










  • It's because that's what you've asked. Give it a try and tell me if it works
    – Alex Mamo
    Nov 20 at 15:54










  • Its still fetching all the data inside sendersid and recievers id
    – user10643013
    Nov 20 at 15:55














0












0








0






If you want to know how many items the value of your Seen property true, please use the following lines of code:



Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("Seen").equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int count = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
count = count + 1;
}
Log.d(TAG, String.valueOf(count)); //Will print the number of seen messages
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
mSeenRef.addListenerForSingleValueEvent(valueEventListener);


As you can see, you just simply need to count the number of occurrences.



Another approach would be to use dataSnapshot.getChildrenCount() which will return a value of type long, which actually represents the number of children that exist within the dataSnapshot object.






share|improve this answer












If you want to know how many items the value of your Seen property true, please use the following lines of code:



Query mSeenRef = db.child("Messages").child(MessageSenderId).child(MessageRecieverId).orderByChild("Seen").equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int count = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
count = count + 1;
}
Log.d(TAG, String.valueOf(count)); //Will print the number of seen messages
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
mSeenRef.addListenerForSingleValueEvent(valueEventListener);


As you can see, you just simply need to count the number of occurrences.



Another approach would be to use dataSnapshot.getChildrenCount() which will return a value of type long, which actually represents the number of children that exist within the dataSnapshot object.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 15:08









Alex Mamo

39k72758




39k72758












  • But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
    – user10643013
    Nov 20 at 15:21










  • Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
    – Alex Mamo
    Nov 20 at 15:31












  • let me make sure... in ur query why is it Seen.equaTo true?
    – user10643013
    Nov 20 at 15:38










  • It's because that's what you've asked. Give it a try and tell me if it works
    – Alex Mamo
    Nov 20 at 15:54










  • Its still fetching all the data inside sendersid and recievers id
    – user10643013
    Nov 20 at 15:55


















  • But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
    – user10643013
    Nov 20 at 15:21










  • Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
    – Alex Mamo
    Nov 20 at 15:31












  • let me make sure... in ur query why is it Seen.equaTo true?
    – user10643013
    Nov 20 at 15:38










  • It's because that's what you've asked. Give it a try and tell me if it works
    – Alex Mamo
    Nov 20 at 15:54










  • Its still fetching all the data inside sendersid and recievers id
    – user10643013
    Nov 20 at 15:55
















But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
– user10643013
Nov 20 at 15:21




But i tried this method and also getChildrenCount but both gives the number of total childs inside but i want the number of childs where seen=false only
– user10643013
Nov 20 at 15:21












Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
– Alex Mamo
Nov 20 at 15:31






Please show the code that produces that behaviour but if you are using a correct reference, it should count only the childs where seen=false, right? Have you tried my solution above? What is the behaviour?
– Alex Mamo
Nov 20 at 15:31














let me make sure... in ur query why is it Seen.equaTo true?
– user10643013
Nov 20 at 15:38




let me make sure... in ur query why is it Seen.equaTo true?
– user10643013
Nov 20 at 15:38












It's because that's what you've asked. Give it a try and tell me if it works
– Alex Mamo
Nov 20 at 15:54




It's because that's what you've asked. Give it a try and tell me if it works
– Alex Mamo
Nov 20 at 15:54












Its still fetching all the data inside sendersid and recievers id
– user10643013
Nov 20 at 15:55




Its still fetching all the data inside sendersid and recievers id
– user10643013
Nov 20 at 15:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395053%2fgetting-count-before-executing-a-method-in-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin