Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count all values Firebase Java API

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.

like image 605
nothingness Avatar asked Oct 19 '22 18:10

nothingness


1 Answers

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));
like image 128
nothingness Avatar answered Oct 22 '22 09:10

nothingness