Count all Firebase Java API values

I am trying to count all the values ​​in a firebase database in Java. But it does not work, I looked through some tutorials for this, but did not find it 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, pay attention to the line in which I tried to use the maximum count 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 from the max function from Collections:

The max (Collection) method in the Collections type is not applicable for arguments (String)

Does anyone know how to solve this problem in Java, thanks in advance.

+5
source share
2 answers

Got this action, 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)); 
+3
source

IT should be:

 int maxValueInMap=(Collections.max(newPost.values())); 

You accidentally made .toString . The string is not a Collection , so .max will not work on it.

However, I could recommend if you can place an order using the limit clause, as it can become more expensive to run Collections.max again and again. I don't see the TOP function in firebase docs, but that would be even better.

0
source

Source: https://habr.com/ru/post/1211964/


All Articles