Reputation: 694
I'm trying to count all the values in my firebase database in Java. But its not working, I have looked at some tutorials for this but haven't found any for Java programmers.
Here is my database:
Language
German
Message
-Jf6ShYy7niHrqg_x4Tc: "Tomorrow is very windy"
-Jf9v0xHAxINUUANrORU: "Today is very windy and rainy"
Here is my code, note the line I attempted to use count max value:
Firebase f = new Firebase("https://myapp.firebaseio.com/Language/German/Message/");
f.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
disp_msg = (TextView)findViewById(R.id.display_msg);
//disp_msg.setText(snapshot.getValue().toString());
Iterable<DataSnapshot> ds = snapshot.getChildren();
Iterator<DataSnapshot> ids = ds.iterator();
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue();
String msg = newPost.get("pubMsg").toString();
disp_msg.setText(msg.toString());
//The line below does not work getting the maximum values
int maxValueInMap=(Collections.max(newPost.values().toString()));
}
This is the error I get at the max function from Collections:
The method max(Collection) in the type Collections is not applicable for the arguments (String)
Does anyone know how to solve this problem in Java, Thanks in advance.
Upvotes: 5
Views: 1916
Reputation: 694
Got it working while inside my firebase child directory, found the firebase function:
long maxNum = snapshot.getChildrenCount();
int nums = (int)maxNum;
disp_msg.setText(msg.toString() + Integer.toString(nums));
Upvotes: 3
Reputation: 24005
IT should be:
int maxValueInMap=(Collections.max(newPost.values()));
You accidentally did .toString
. A String is not a Collection
, and therefore .max
will not work on it.
I might recommend however, seeing if you can do an order by with limit clause, as it could get expensive to run Collections.max
again and again. I can't see a TOP
function in the firebase docs, but this would be even better.
Upvotes: 0