简体   繁体   中英

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

How to implement the interface for displaying a graph with data from 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...

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

Which is the best way to plot this data?

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

This code seems giving the error. Here bardataset is already the same type with IBarDataSet. So the type of DataTimeSpentArrayList is not compatible with IBarDataSet.

In addition, i would suggest you to use if ( WaterSpentArrayList.size().=0) instead of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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