在Android折线图的xAxis中显示日期

时间:2018-07-07 02:26:01

标签: java android date graph

我正在尝试在两个不同的折线图中显示从Firestore集合文档中检索到的一些值。我使用jjoe64库,但是当我按下按钮生成图形时,我的应用程序崩溃了。 Firestore database这是我的片段代码:

package com.example.drodo.diamondbeacons;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.helper.StaticLabelsFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class TimelineFragment extends Fragment {

    private FirebaseFirestore firebaseFirestore;
    private FirebaseAuth firebaseAuth;
    private EditText regionText;
    private Button regionChooseBtn;


    public TimelineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_timeline, container, false);

        regionText = (EditText) view.findViewById(R.id.selectRegionText);
        regionChooseBtn = (Button) view.findViewById(R.id.chooseRegionBtn);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseFirestore = FirebaseFirestore.getInstance();
        regionChooseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String select_text = regionText.getText().toString();
                if (!select_text.isEmpty()) {

                    if(firebaseAuth.getCurrentUser() != null) {

                        firebaseFirestore.collection("PuckData").whereEqualTo("region", select_text)
                                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()) {
                                    List<String> dates_list = new ArrayList<>();
                                    List<Integer> temperature_list = new ArrayList<>();
                                    List<Integer> light_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-HH:mm");
                                        String stringDate = simpleDateFormat.format(date);

                                        temperature_list.add(temperature);
                                        light_list.add(light);
                                        dates_list.add(stringDate);
                                    }

                                    GraphView graph = view.findViewById(R.id.graph);
                                    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(generateLightList(dates_list, light_list));
                                    graph.addSeries(series);
                                    Log.d("LIST", "DATES: " + dates_list.toString());
                                    Log.d("LIST", "TEMPS: " + temperature_list.toString());
                                    Log.d("LIST", "LIGHTS: " + light_list.toString());
                                } else {
                                    Log.d("PUCK", "Error getting documents: ", task.getException());
                                }
                            }
                        });

                    }
                }

                regionText.setText("");

                final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
            }
        });
        return view;
    }

    private DataPoint[] generateLightList(List<String> dateList, List<Integer> lightList) {
        DataPoint[] values = new DataPoint[dateList.size()];
        for (int i = 0; i < dateList.size(); i++) {
            DataPoint v = new DataPoint(Double.parseDouble(dateList.get(i)), lightList.get(i));
            values[i] = v;
        }
        return values;
    }

    private DataPoint[] generateTemperatureList(List<String> dateList, List<Integer> temperatureList) {
        DataPoint[] values = new DataPoint[dateList.size()];
        for (int i = 0; i < dateList.size(); i++) {
            DataPoint v = new DataPoint(Double.parseDouble(dateList.get(i)), temperatureList.get(i));
            values[i] = v;
        }
        return values;
    }

}

这是我的模型PuckData类:

package com.example.drodo.diamondbeacons.model;

import com.google.firebase.firestore.ServerTimestamp;

import java.util.Date;
/**
 * Created by drodo on 25-Mar-18.
 */

public class PuckData {

    public int light, temperature;
    public double battery;
    public Date timestamp;


    public PuckData() {}

    public PuckData(int light, int temperature, double battery, Date timestamp) {
        this.light = light;
        this.temperature = temperature;
        this.battery = battery;
        this.timestamp = timestamp;
    }

    public double getLight() {
        return light;
    }

    public void setLight(int light) {
        this.light = light;
    }

    public double getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    public double getBattery() {
        return battery;
    }

    public void setBattery(double battery) {
        this.battery = battery;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

}

我想在第一张图中显示xAxis(dates)和yAxis(light)以及另一张图中的dates-temperature。当我按下按钮以仅选择用户在编辑文本中键入的区域时,我的应用程序崩溃了,这就是我得到的:

  

java.lang.NumberFormatException:对于输入字符串:“ 07/07 / 18-04:50”           在java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)           在java.lang.Double.parseDouble(Double.java:547)           在com.example.drodo.diamondbeacons.TimelineFragment.generateLightList(TimelineFragment.java:118)

1 个答案:

答案 0 :(得分:1)

错误消息很清楚。您正在呼叫Double.parseDouble(dateList.get(i)),但是dateList.get(i)不是数字,而是格式为"07/07/18-04:50"的字符串。

我的猜测是您正在尝试调用this constructor of DataPointpublic DataPoint(Date x, double y) {...,这意味着您应该将字符串解析为其相应的数据。

Date date=new SimpleDateFormat("dd/MM/yyyy-HH:mm").parse(dateList.get(i));
DataPoint v = new DataPoint(date, lightList.get(i));