简体   繁体   English

如何创建 ArrayList<barentry> 使用数组<integer> ?</integer></barentry>

[英]How to create an ArrayList <BarEntry> using array <Integer>?

How to implement the interface for displaying a graph with data from SQLlite?如何实现显示带有 SQLlite 数据的图形的界面?

 List<Integer> WaterSpentArrayList;
WaterSpentArrayList = db.getWaterSpent();
    ArrayList<BarEntry> EntryData = new ArrayList<>();
    if ( WaterSpentArrayList.size()==0) {
    }else {
        for (int i = 0; i < WaterSpentArrayList.size(); i++) {
            EntryData.add(new BarEntry(WaterSpentArrayList.get(i), i));
        }
        Log.e(TAG, "Data:  " + EntryData);
        BarDataSet bardataset = new BarDataSet(EntryData, "WaterSpentArrayList");
        BarData data = new BarData((IBarDataSet) DataTimeSpentArrayList, bardataset);
        barChart.setData(data);here

I put values of List and these number to ArrayList but this leads to an error, it does not allow to draw a graph...我将 List 的值和这些数字放入 ArrayList 但这会导致错误,它不允许绘制图表...

E/CleanersActivity: SimpleEntry: [Entry, x: 20.0 y: 0.0, Entry, x: 29.0 y: 1.0, Entry, x: 35.0 y: 2.0, Entry, x: 22.0 y: 3.0, Entry, x: 20.0 y: 4.0, Entry, x: 29.0 y: 5.0, Entry, x: 35.0 y: 6.0, Entry, x: 22.0 y: 7.0, Entry, x: 20.0 y: 8.0, Entry, x: 29.0 y: 9.0, Entry, x: 35.0 y: 10.0, Entry, x: 22.0 y: 11.0, Entry, x: 20.0 y: 12.0, Entry, x: 29.0 y: 13.0, Entry, x: 35.0 y: 14.0, Entry, x: 22.0 y: 15.0] D/AndroidRuntime: Shutting down VM --------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: PID: 28873 java.lang.RuntimeException: Unable to start activity ComponentInfo{CleanersActivity}: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.mikephil.charting.interfaces.datasets.IBarDataSet E/CleanersActivity: SimpleEntry: [Entry, x: 20.0 y: 0.0, Entry, x: 29.0 y: 1.0, Entry, x: 35.0 y: 2.0, Entry, x: 22.0 y: 3.0, Entry, x: 20.0 y: 4.0, 入口, x: 29.0 y: 5.0, 入口, x: 35.0 y: 6.0, 入口, x: 22.0 y: 7.0, 入口, x: 20.0 y: 8.0, 入口, x: 29.0 y: 9.0, 入口, x : 35.0 y: 10.0, 入口, x: 22.0 y: 11.0, 入口, x: 20.0 y: 12.0, 入口, x: 29.0 y: 13.0, 入口, x: 35.0 y: 14.0, 入口, x: 22.0 y: 15.0 ] D/AndroidRuntime: 关闭 VM --------- 崩溃开始 E/AndroidRuntime: 致命例外: 主进程: PID: 28873 java.lang.RuntimeException: 无法启动活动 ComponentInfo{CleanersActivity}: Z93F725A07423FE1C889F448B33D2。 lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.mikephil.charting.interfaces.datasets.IBarDataSet

Which is the best way to plot this data? plot 这个数据的最佳方法是什么?

BarData data = new BarData((IBarDataSet) DataTimeSpentArrayList, bardataset);

This code seems giving the error.这段代码似乎给出了错误。 Here bardataset is already the same type with IBarDataSet.这里的 bardataset 已经和 IBarDataSet 是同一个类型。 So the type of DataTimeSpentArrayList is not compatible with IBarDataSet.所以 DataTimeSpentArrayList 的类型与 IBarDataSet 不兼容。

In addition, i would suggest you to use if ( WaterSpentArrayList.size().=0) instead of if ( WaterSpentArrayList.size()==0) { }else.另外,我建议您使用 if ( WaterSpentArrayList.size().=0) 而不是 if ( WaterSpentArrayList.size()==0) { }else。 Also using a variable for WaterSpentArrayList.size() is the best practice since the program has to calculate the size value every time checking the condition.同样为 WaterSpentArrayList.size() 使用变量是最佳实践,因为程序必须在每次检查条件时计算大小值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM