我有一个由ArrayList填充的lineGraph。我在一个屏幕上显示6个图表,每个图表由方法调用/切换语句触发。问题是,当调用下一个图表时,它仍然具有上一个图表中的信息,因此它“加倍”并继续执行此操作,直到程序崩溃为止。我实际上对编码没有经验,我希望有人可以看看我的代码并查明我的逻辑错误。
请在下面查看我的代码段。
private ArrayList<Entry> yVals;
@Override
public void onClick (View view){
switch (view.getId()) {
case R.id.moodLogQuarterly:
retroFitQuarterly();
break;
case R.id.moodLogYearly:
retroFitYearly();
break;
public void retroFitYearly() {
// Retrofit call to the MYSQL server.
Call<List<MoodLogLineGraph>> call = RetrofitClient.getInstance().getApi().moodLogLineGraphYearly(userId);
call.enqueue(new Callback<List<MoodLogLineGraph>>() {
@Override
public void onResponse(Call<List<MoodLogLineGraph>> call, Response<List<MoodLogLineGraph>> response) {
getLineGraphWithResponse(response);
}
public void retroFitQuarterly() {
// Retrofit call to the MYSQL server.
Call<List<MoodLogLineGraph>> call = RetrofitClient.getInstance().getApi().moodLogLineGraphQuarterly(userId);
call.enqueue(new Callback<List<MoodLogLineGraph>>() {
@Override
public void onResponse(Call<List<MoodLogLineGraph>> call, Response<List<MoodLogLineGraph>> response) {
getLineGraphWithResponse(response);
}
private void getLineGraphWithResponse(Response<List<MoodLogLineGraph>> response) {
// All JSON entries form the MYSQL server are parsed into an ArrayList.
yVals = new ArrayList<>();
// For loop which dynamically adds all entries to the ArrayList
// response.body() contains the JSON parsed from the database. The JSON
// contains all the information requested from the database such as moodId,
// moodName, posted(date) etc.
// Getters are used to select specific pieces of information form the JSON array.
for (MoodLogLineGraph moodLogList : response.body()) {
// The getter .getMoodBefore contains an int between 1-11. These ints represent
// the mood the user has inputted to the database i.e 1=happy, 11= Depressed etc.
// The float val is used to store the integer so it can used in a LineGraph.
val = (float) moodLogList.getMoodBefore();
}
// Obtains the date the mood was recorded from the MYSQL database.
date = (float) moodLogList.getDay();
// Adds the values and dates to the LineGraph ArrayList.
yVals.add(new Entry(date, val));
// Works out the percentage of positive moods
percentage = total / response.body().size();
moodLogPercentage.setText(Math.round(percentage * 100) + "%" + " of your moods are positive");
//Initialising the LineChart variables which control the presentation.
LineDataSet set1 = new LineDataSet(yVals, "Data set1");
set1.setColor(Color.WHITE);
set1.setLineWidth(2f);
set1.setDrawValues(false);
set1.setDrawCircles(false);
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set1.setCubicIntensity(0.1f);
ArrayList<ILineDataSet> dataSet = new ArrayList<>();
dataSet.add(set1);
LineData data = new LineData(dataSet);
***其余的代码只是用于设置折线图样式的变量。