带有Piecharts的Android GridView,UnsupportedOperationException:addView(View,LayoutParams)

时间:2014-12-29 10:17:11

标签: android

我想使用Achartengine在 Gridview 中显示饼图

它说 java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams) 我不知道为什么,一些建议PLZ ....

我的适配器类:

public class GraphAdapter extends BaseAdapter {
    class ViewHolder {

        private LinearLayout layout;
        private int[] COLORS = new int[] { Color.GREEN, Color.BLUE,
                Color.MAGENTA, Color.CYAN };
        private CategorySeries mSeries;
        private DefaultRenderer mRenderer;
        private GraphicalView mChartView;

        public ViewHolder(View view) {
            layout = (LinearLayout) view.findViewById(R.id.adapter_dash_graph);
            mRenderer = new DefaultRenderer();
            mSeries = new CategorySeries("");
            mChartView = ChartFactory.getPieChartView(mContext, mSeries,
                    mRenderer);
        }

        private void updateChild(String[] names, int[] values) {
            renderGraph(names, values);
            // Draw chart
            mChartView.repaint();
            layout.addView(mChartView);
        }

        private void renderGraph(String[] names, int[] values) {
            mRenderer.setApplyBackgroundColor(true);
            mRenderer.setBackgroundColor(mContext.getResources().getColor(
                    android.R.color.white));
            mRenderer.setChartTitleTextSize(20);
            mRenderer.setLabelsTextSize(20);
            mRenderer.setLabelsColor(mContext.getResources().getColor(
                    R.color.blue_dark));
            mRenderer.setLegendTextSize(20);
            mRenderer.setShowLegend(true);
            mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
            mRenderer.setZoomButtonsVisible(false);
            mRenderer.setStartAngle(90);
            mRenderer.setZoomEnabled(false);
            mRenderer.setPanEnabled(false);
            for (int i = 0; i < values.length; i++) {
                mSeries.add(names[i] + " " + values[i], values[i]);
                SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
                renderer.setColor(COLORS[(mSeries.getItemCount() - 1)
                        % COLORS.length]);
                mRenderer.addSeriesRenderer(renderer);
            }
        }
    }

    private Context mContext;
    private List<OverallDetails> mUserMenus;

    public GraphAdapter(Context context, List<OverallDetails> slideMenus) {

        this.mContext = context;
        this.mUserMenus = slideMenus;
    }

    @Override
    public int getCount() {
        return mUserMenus.size();
    }

    @Override
    public OverallDetails getItem(int position) {
        return mUserMenus.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup group) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_graph, group);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        viewHolder.updateChild(
                mContext.getResources().getStringArray(R.array.graph_name),
                mContext.getResources().getIntArray(R.array.graph_values));
        return convertView;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试以这种方式实现ViewHolder:

public class GraphAdapter extends BaseAdapter {

    private int[] COLORS = new int[] { Color.GREEN, Color.BLUE,Color.MAGENTA, Color.CYAN };
    private Context mContext;
    private List<OverallDetails> mUserMenus;

    public GraphAdapter(Context context, List<OverallDetails> slideMenus) {
        this.mContext = context;
        this.mUserMenus = slideMenus;
    }

    @Override
    public int getCount() {
        return mUserMenus.size();
    }

    @Override
    public Objects getItem(int position) {
        return mUserMenus.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup group) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_graph, null);
            viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.adapter_dash_graph);
            convertView.setTag(viewHolder);

        }else{
           viewHolder = (ViewHolder) convertView.getTag();
        }

        DefaultRenderer mRenderer = new DefaultRenderer();
        CategorySeries mSeries = new CategorySeries("");
        GraphicalView mChartView = ChartFactory.getPieChartView(mContext, mSeries,mRenderer);
        renderGraph(mRenderer,mSeries,mContext.getResources().getStringArray(R.array.graph_name),mContext.getResources().getIntArray(R.array.graph_values));
        // Draw chart
        mChartView.repaint();
        viewHolder.layout.removeAllViews();
        viewHolder.layout.addView(mChartView);

        return convertView;
    }

    class ViewHolder{
        LinearLayout layout;
    }
    private void renderGraph(DefaultRenderer mRenderer,CategorySeries mSeries,String[] names, int[] values) {
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setLabelsTextSize(20);
        mRenderer.setLabelsColor(mContext.getResources().getColor(R.color.blue_dark));
        mRenderer.setLegendTextSize(20);
        mRenderer.setShowLegend(true);
        mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
        mRenderer.setZoomButtonsVisible(false);
        mRenderer.setStartAngle(90);
        mRenderer.setZoomEnabled(false);
        mRenderer.setPanEnabled(false);

        for (int i = 0; i < values.length; i++) {
            mSeries.add(names[i] + " " + values[i], values[i]);
            SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
            renderer.setColor(COLORS[(mSeries.getItemCount() - 1)% COLORS.length]);
            mRenderer.addSeriesRenderer(renderer);
        }
    }

}