如何动态地将数据从 firebase 添加到 graphview?

How to add data from firebase to graphview dynamically?

我正在使用 jjoe64.graphview.GraphView 库来绘制图表。

我正在从 firebase 数据库附加一些数据,但显示错误。

这是我的代码:

 FirebaseDatabase realdata = FirebaseDatabase.getInstance();
        DatabaseReference real = realdata.getReference("weight");
        ValueEventListener workplz = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

//point value=62,pointvalue1=76
                pointValue = Integer.parseInt(String.valueOf(dataSnapshot.child("currentweight").getValue()));
                pointValue1 = Integer.parseInt(String.valueOf(dataSnapshot.child("goalweight").getValue()));
                Log.i("xValue", String.valueOf(pointValue));
                Log.i("yValue", String.valueOf(pointValue1));


                valuedata = Double.parseDouble(String.valueOf(dataSnapshot.child("currentweight").getValue()));
                hello();
                }
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }};
        real.addValueEventListener(workplz);

        GraphView graph = findViewById(R.id.graph);
        LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(1, 5),
                new DataPoint(2, 3)
        });
        graph.addSeries(series);
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(0);
        graph.getViewport().setMaxY(200);
        graph.getViewport().setMaxX(0);
        graph.getViewport().setMaxX(200);
        //appending data 
        series.appendData(new DataPoint(pointValue, pointValue1), true, 200);

虽然 运行 我收到这个错误的应用程序。

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    at com.example.itsandpro.Signup.GraphActivity.onCreate(GraphActivity.java:97)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我想将这两个值添加到 graph.I 也尝试在 double 中使用这些值但错误仍然相同:

   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
    at com.example.itsandpro.Signup.GraphActivity.onCreate(GraphActivity.java:85)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

根据您的评论,您说您获得了这些值但仍然收到错误,是因为 Firebase API 是异步的,这意味着当您创建以下对象时:

new DataPoint(pointValue, pointValue1), true, 200

来自数据库的数据尚未完成加载。一个快速解决这个问题的方法是移动所有这些代码行:

GraphView graph = findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
    new DataPoint(0, 1),
    new DataPoint(1, 5),
    new DataPoint(2, 3)
});
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(0);
graph.getViewport().setMaxX(0);
graph.getViewport().setMaxX(200);
//appending data 
series.appendData(new DataPoint(pointValue, pointValue1), true, 200);

onDataChange() 方法中,否则我建议您从这个 in which I have explained how it can be done using a custom callback. You can also take a look at this video 中查看我的答案的最后一部分,以便更好地理解。