안녕하세요.
MPAndroidChart 를 이용하여 데이터를 실시간으로 그리는 어플을 만들고 있습니다.
총 7개의 item이 있구요.
각 item마다 차트가 하나씩 들어가 있습니다.
문제는 스크롤하여 나머지 리스트를 보고 다시 스크롤하여 위로 올리면 원래 보고 있던 차트의
데이터가 아닌 아래쪽에서 봤었던 데이터를 그리고 있는 등 이상한 오작동을 하고 있습니다.
아래는 BaseAdapter를 상속한 ChartListVuewAdapter 소스입니다.
package com.exem.app.maxgaugeformysql;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.exem.app.maxgaugeformysql.chart.XmLineChart;
import com.exem.app.maxgaugeformysql.common.Consts;
import com.github.mikephil.charting.charts.LineChart;
import java.util.ArrayList;
public class ChartListViewAdapter extends BaseAdapter {
private ChartList item;
ArrayList<ChartList> chartLists;
Holder holder = null;
public ChartListViewAdapter(ArrayList<ChartList> data) {
this.chartLists = data;
}
public void loadData() {
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return Consts.chartLists.size();
}
@Override
public Object getItem(int position) {
return chartLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int pos, View convertView, ViewGroup parent) {
boolean lineChartVisible = chartLists.get(pos).getLineChartVisible();
Context context = parent.getContext();
TextView lineStatName = null;
TextView areaStatName = null;
ChartList chartList = null;
LineChart lineChart = null;
LinearLayout lineChartBk = null;
String statname = chartLists.get(pos).getStatname();
// 리스트가 길어지면서 화면에 보이지 않는 아이템은 convertView가 null인 상태로 들어옴
if (convertView == null) {
Log.d("NULL", statname + " / pos:" + pos);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.chart_list_view_item, parent, false);
item = (ChartList) chartLists.get(pos);
if (item.getXmLineChart() == null) {
item.setXmLineChart(new XmLineChart(parent.getContext(), convertView.findViewById(R.id.linechart)));
}
lineStatName = (TextView) convertView.findViewById(R.id.linestatname);
lineChart = (LineChart) convertView.findViewById(R.id.linechart);
lineChartBk = (LinearLayout) convertView.findViewById(R.id.lineChartBk);
chartList = chartLists.get(pos);
holder = new Holder();
holder.lineStatName = lineStatName;
holder.chartList = chartList;
holder.lineChart = lineChart;
holder.lineChartBk = lineChartBk;
convertView.setTag(holder);
} else {
Log.d("NOT NULL", statname);
holder = (Holder) convertView.getTag();
lineStatName = holder.lineStatName;
chartList = holder.chartList;
lineChart = holder.lineChart;
lineChartBk = holder.lineChartBk;
}
Log.d("NORMAL", statname);
if (chartLists.get(pos).getXmLineChart() != null) {
lineChart = chartLists.get(pos).getXmLineChart().lineChart;
}
holder.lineStatName.setText(chartLists.get(pos).getStatname());
holder.lineStatName.setTypeface(Consts.robotoCondensedLight);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean lineChartVisible = chartLists.get(pos).getLineChartVisible();
if (lineChartVisible) {
chartLists.get(pos).setLineChartVisible(false);
notifyDataSetChanged();
} else {
chartLists.get(pos).setLineChartVisible(true);
notifyDataSetChanged();
}
}
});
return convertView;
}
private class Holder {
TextView lineStatName;
ChartList chartList;
LineChart lineChart;
LinearLayout lineChartBk;
}
}
제가 생각하기엔 이 커스텀어뎁터를 잘못 만든것 같은데요.
어디가 문제인지 잘 모르겠습니다.
고수님들의 도움이 필요합니다.
감사합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="#FFFFFF"
android:id="@+id/lineChartBk">
<LinearLayout
android:orientation="vertical"
android:layout_width="90dp"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="CPU"
android:id="@+id/linestatname"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/chartbackground">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/linechart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>