значения соответствуют случайным меткам и не могут очистить старые значения. Я получаю такой же вывод в ярлыках. ![enter image description here](https://i.stack.imgur.com/LBEB5.png)
мой динамический JSON
{"sucessfull_count": "5", "count": [{"count_name": "молитва", "count_no.": "5"}, {"count_name": "death_day", "count_no.": "8"}, {"count_name": "Rosary", "count_no.": "4"}, {"count_name": "Marriage", "count_no.": "2"}]}
public void dashboardCounts ()
{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(json)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Check your Internet Connection..", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(@NonNull Call call, @NonNull final Response response) throws IOException {
final String java = response.body().string();
new Handler(Looper.getMainLooper()).post(new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
labels.clear();
recentDashBoardCounts.clear();
try {
JSONObject jsonObject = new JSONObject(java);
success_count = jsonObject.getString("sucessfull_count");
Log.e("test cnt suc",success_count);
success_count_textview.setText(success_count);
JSONArray jsonArray = jsonObject.getJSONArray("count");
array_size = jsonArray.length();
for(int i = 0; i<array_size; i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
count_name = jsonObject1.getString("count_name");
count_no = jsonObject1.getString("count_no.");
Log.e("test cnt",count_name+" "+count_no);
labels.add(count_name);
recentDashBoardCounts.put("name"+i,count_name);
recentDashBoardCounts.put("no"+i,count_no);
Log.e("test cnt hash",recentDashBoardCounts.get("name"+i)+" "+recentDashBoardCounts.get("no"+i));
}
pieChart();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
}
private void pieChart () {
for(int i =0; i<recentDashBoardCounts.size()/2; i++) {
Log.e("hashmap length/2", "" + recentDashBoardCounts.size()/2+ "label name "+ labels);
labels.add(recentDashBoardCounts.get("name" + i));
}
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.getDescription().setEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
mChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(labels);
xl.setValueFormatter(xaxisFormatter);
xl.setGranularity(1);
YAxis yl = mChart.getAxisLeft();
yl.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yl.setDrawGridLines(false);
yl.setEnabled(false);
yl.setAxisMinimum(0f);
yl.setDrawAxisLine(false);
YAxis yr = mChart.getAxisRight();
yr.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f);
yr.setEnabled(false);
yr.setDrawLabels(false);
yr.setDrawAxisLine(false);
ArrayList<BarEntry> yVals1 = new ArrayList<>();
Log.e("test yVals",""+ yVals1);
for(int i =0; i<array_size;i++) {
int c = Integer.parseInt(recentDashBoardCounts.get("no" + i));
Log.e("value1", "" + c + recentDashBoardCounts.get("name" + i));
yVals1.add(new BarEntry((float) c, (float)Integer.parseInt(recentDashBoardCounts.get("no" + i))));
}
BarDataSet set1;
set1 = new BarDataSet(yVals1, "DataSet 1");
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(.9f);
mChart.setData(data);
mChart.setScaleEnabled(false);
mChart.getLegend().setEnabled(false);
}
открытый класс CategoryBarChartXaxisFormatter реализует IAxisValueFormatter {
ArrayList<String> mValues;
public CategoryBarChartXaxisFormatter(ArrayList<String> values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int) value;
String label = "";
if (val >= 0 && val < mValues.size()) {
label = mValues.get(val);
} else {
label = "";
}
return label;
}
}