마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

MPAndroidChart로 리스트뷰를 만들었는데 스크롤하여 내렸다가 다시 올리면 다른 값을 그리는 문제가 있습니다. ㅠㅠ

0 추천

안녕하세요.

 

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>

 

케이엔비 (660 포인트) 님이 2015년 12월 11일 질문
케이엔비님이 2015년 12월 11일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...