-1

I have a collection named PuckData where I store some int values(temperature, light) and timestamp. I would like to display those values in line graphs with Android Graph Library(jjoe64). The first graph will be temperature(yAxis)-timestamp(xAxis) and the second will be light-timestamp. I want the timestamp values to be displayed in this format: dd:mm:yyyy. In the code below I receive the values of temperature and light. I want to get timestamp values also.

firebaseFirestore.collection("PuckData").whereEqualTo("region", "Bedroom")
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                            int light = document.getLong("light").intValue();
                            int temperature = document.getLong("temperature").intValue();
                            Log.d("PUCK", "LIGHT: " + light);

                }
            } else {
                Log.d("PUCK", "Error getting documents: ", task.getException());
            }
        }
    });

PuckData Collection Image How is it possible to add them in ArrayLists and pass them as Data points? Thanks in advance!

diamond
  • 13
  • 1
  • 6
  • Please add your database structure. – Alex Mamo Jul 04 '18 at 07:31
  • I have my PuckData collection and inside it I have documents with the fields: light, temperature(int values), battery(double), region(String), timestamp and the user_id, but I need only to display in graphs the temperature - timestamp and light - timestamp. PuckData is a root folder. Hope this helps – diamond Jul 04 '18 at 10:52
  • It doesn't. Please a link to a screenshot. – Alex Mamo Jul 04 '18 at 10:55
  • I did put a link under my code. – diamond Jul 04 '18 at 11:04

2 Answers2

0

In order to get timestamp use:-

Date lightTimestamp =  document.getDocument().getDate("light-timestamp");

And then you can convert this date to any format you want.

Adding in arraylist:- You have to create an object with these three attributes. Set data in them and pass them in arraylist.

Raj
  • 2,997
  • 2
  • 12
  • 30
0

I want to get timestamp values also.

To solve this, please use the following code:

Date date = document.getDate("timestamp");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
String stringDate = simpleDateFormat.format(date);
Log.d("DATE", "timestamp: " + stringDate);

The output will be the date in dd/MM/yy format.

Edit:

firebaseFirestore.collection("PuckData").whereEqualTo("region", "Bedroom")
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                List<String> list = new ArrayList<>();
                for (DocumentSnapshot document : task.getResult()) {
                            int light = document.getLong("light").intValue();
                            int temperature = document.getLong("temperature").intValue();
                            Date date = document.getDate("timestamp");
                            SimpleDateFormat simpleDateFormat = new 
                            SimpleDateFormat("dd/MM/yy");
                            String stringDate = simpleDateFormat.format(date);
                            list.add(stringDate);
                            Log.d("DATE", "timestamp: " + stringDate);
                            Log.d("PUCK", "LIGHT: " + light);

                }
                Log.d("LIST", "list: " + list.toString());
            } else {
                Log.d("PUCK", "Error getting documents: ", task.getException());
            }
        }
    });
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193