使用日历作为X轴从sqlite创建图形

时间:2018-08-19 13:27:30

标签: android sqlite android-graphview

我想创建一个相对于整数的图形日历。在表中输入日期格式的键或将字符串值解析为日期。

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VIewGraph extends AppCompatActivity {

    LineGraphSeries<DataPoint> series;
    DatabaseHelper mDatabaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_graph);
        mDatabaseHelper = new DatabaseHelper(this);

        String y;
        Float z;
        Date d1;
        Cursor data = mDatabaseHelper.readEntry();
        int rows = data.getCount();
        data.moveToFirst();

        GraphView graph = (GraphView) findViewById(R.id.graph11);
        series = new LineGraphSeries<DataPoint>();

        for(int i = 0;  i <rows; i++){
            data.moveToNext();
            String x = data.getString(2);
            y = data.getString(3);
            z = Float.parseFloat(y);   
            Date date1;
            try { 
                date1 = new SimpleDateFormat("dd/MM/yyyy").parse(x);
                 } catch (Exception e) {
                   e.printStackTrace();
                  }

            series.appendData(new DataPoint(date1, z), true, 25);
        }
        graph.addSeries(series);

        graph.getGridLabelRenderer().setNumHorizontalLabels(3);
        graph.getGridLabelRenderer().setHumanRounding(false);

    }
}

以下是有关getTime()的代码

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.sql.Time;

public class Logging extends AppCompatActivity {

private static final String TAG = "Logging";

DatabaseHelper mDatabaseHelper;
EditText editWeight, editFat, editHydration, editMuscle, editBone, editTime, editASD;
private Button button2;
private TextView mDisplayDate, mDisplayTime;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private TimePickerDialog.OnTimeSetListener mTimeSetListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logging);
    button2 = (Button) findViewById(R.id.button2);
    mDisplayDate = (TextView) findViewById(R.id.tvDate);
    mDatabaseHelper = new DatabaseHelper(this);
    mDisplayTime = (TextView) findViewById(R.id.tvTime);

    editWeight = (EditText) findViewById(R.id.editWeight);
    editFat = (EditText) findViewById(R.id.editFat);
    editHydration = (EditText) findViewById(R.id.editHydration);
    editMuscle = (EditText) findViewById(R.id.editMuscle);
    editBone = (EditText) findViewById(R.id.editBone);

    mDisplayDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(
                    Logging.this,
                    android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                    mDateSetListener,
                    year,month,day);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();

        }
    });

    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            Log.d(TAG, "onDateSet: mm/dd/yyyy: " + day + "/" + month + "/" + year);

            String date = day + "/" + month + "/" + year;
            mDisplayDate.setText(date);
        }
    };

    mDisplayTime.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            Calendar mTime = Calendar.getInstance();
                                            int mHour = mTime.get(Calendar.HOUR_OF_DAY);
                                            int mMinute = mTime.get(Calendar.MINUTE);

                                            TimePickerDialog timePickerDialog = new TimePickerDialog(Logging.this,
                                                    new TimePickerDialog.OnTimeSetListener() {

                                                        @Override
                                                        public void onTimeSet(TimePicker view, int hourOfDay,
                                                                              int minute) {

                                                            mDisplayTime.setText(hourOfDay + ":" + minute);
                                                        }
                                                    }, mHour, mMinute, true);
                                            timePickerDialog.show();
                                        }
                                    });

    LogData();
}

public void LogData(){
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isInserted = mDatabaseHelper.addData(
                    mDisplayDate.getText().toString(),
                    mDisplayTime.getText().toString(),
                    editWeight.getText().toString(),
                    editFat.getText().toString(),
                    editHydration.getText().toString(),
                    editMuscle.getText().toString(),
                    editBone.getText().toString()
            );
            if (isInserted == true)
                Toast.makeText(Logging.this, "Data Inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(Logging.this, "Data not Inserted", Toast.LENGTH_LONG).show();
        }
    });
}

}

现在,错误在Logcat上出现:

08-19 21:58:50.313 25858-25858 / example.christopher.bd E / Android运行时:致命异常:主要     进程:example.christopher.bd,PID:25858     java.lang.RuntimeException:无法启动活动ComponentInfo {example.christopher.bd/example.christopher.bd.VIewGraph}:java.lang.NullPointerException:尝试调用虚拟方法'long java.util.Date.getTime()'在空对象引用上         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)         在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)         在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)         在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)         在android.os.Handler.dispatchMessage(Handler.java:106)         在android.os.Looper.loop(Looper.java:193)         在android.app.ActivityThread.main(ActivityThread.java:6669)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)      原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'long java.util.Date.getTime()'         在com.jjoe64.graphview.series.DataPoint。(DataPoint.java:45)         在example.christopher.bd.VIewGraph.onCreate(VIewGraph.java:48)         在android.app.Activity.performCreate(Activity.java:7136)         在android.app.Activity.performCreate(Activity.java:7127)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)         在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)         在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)         在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)         在android.os.Handler.dispatchMessage(Handler.java:106)         在android.os.Looper.loop(Looper.java:193)         在android.app.ActivityThread.main(ActivityThread.java:6669)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

有人可以帮助我吗?谢谢

2 个答案:

答案 0 :(得分:0)

更改此行:

obs_id

收件人:

let arr = [{key : value},{key : value}];

这应该可以解决您的问题。

解析要求它处于尝试状态,因为 Parse 不知道将提供哪种字符串。

答案 1 :(得分:0)

请参见ViewGraph Activity文件中的导入语句:

import java.sql.Time; // Delete this line

写这行:

import java.util.Time;

现在可以使用。