Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 when trying to clear an array of Entries for LineChart Android

I'm trying to clear an Array of Entries passed when I create LineDataSet for LineChart and I'm still getting this error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Can you tell me please the reason? I tried different solutions but nothing worked so far. Thanks!

Function:

private fun setCashData(cashList: MutableList<CashCollection.CashCollectionItem>, daysNmb: Int) {
    val dateArrayList: ArrayList<String> = arrayListOf()
    val reverseCashList: MutableList<CashCollection.CashCollectionItem> = arrayListOf()
    var i = 0F
    val listIterator = cashList.listIterator()
    while (listIterator.hasNext()) listIterator.next()
    while (listIterator.hasPrevious()) reverseCashList.add(listIterator.previous())

    for (item in reverseCashList) {
        if (i <= daysNmb) {
            var cashValue: String = item.value!!.replaceFirst(",", ".")
            var date: String = item.label!!.replace(".2020", "")
            if (cashValue.toFloat() != 0F) {
                dataCashEntries.add(Entry(i, cashValue.toFloat()))
                dateArrayList.add(date)
                i++
            }
        }
    }

    var lineDataSet1 = LineDataSet(dataCashEntries, "Cash Collection")

    //HERE IS THE PROBLEM
    dataCashEntries.clear()

    Log.i("RAZZZVI", "Set cash data $dataCashEntries")
    lineDataSet1.setCircleColor(getChartsColors()[0])
    lineDataSet1.color = getChartsColors()[0]
    lineDataSet1.circleSize = 5f
    lineDataSet1.lineWidth = 3f
    lineDataSet1.fillAlpha = 160
    lineDataSet1.setDrawFilled(true)
    lineDataSet1.fillColor = getChartsColors()[0]
    lineData.addDataSet(lineDataSet1)
    lineData.setValueTextSize(10f)
    line_chart_details.data = lineData
    line_chart_details.description.isEnabled = false
    val xAxis = line_chart_details.xAxis
    val yAxisRight = line_chart_details.axisRight
    val yAxisLeft = line_chart_details.axisLeft
    yAxisRight.isEnabled = false
    yAxisLeft.isEnabled = true
    yAxisLeft.setDrawLabels(true)
    yAxisLeft.setDrawGridLines(true)
    xAxis.position = XAxis.XAxisPosition.BOTTOM
    xAxis.isEnabled = true
    line_chart_details.xAxis.valueFormatter = IndexAxisValueFormatter(dateArrayList)
    //line_chart.xAxis.mAxisRange = barLabesArrayList.size.toFloat()
    line_chart_details.isClickable = false
    line_chart_details.isHighlightPerTapEnabled = false
    line_chart_details.onTouchListener = null
    line_chart_details.legend.isEnabled = true
    line_chart_details.animateY(2000)
    lineData.notifyDataChanged()
    line_chart_details.notifyDataSetChanged()
    line_chart_details.invalidate()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...