Android开发之记账本开发第四天

一、说在前面

昨天完成了完成数据库的部署和对业务逻辑的初步整理,今天对业务逻辑进行进一步的完善,遇到了很大的问题。今天想要在记账本中添加一个图表来显示数据的信息,但是最后也没能解决了,休息了明天解决!明天再解决了这个问题的基础上再在功能上添加一些细节。

二、今天完成的源代码

package com.example.daliy;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

public class ChartActivity extends Activity {


    private LineChartView mchart;
    private Map<String, Integer> table = new TreeMap<>();
    private LineChartData mdata;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart_view);
        mchart = findViewById(R.id.chart);
        List<CostBean> allData = (List<CostBean>) getIntent().getSerializableExtra("cost_list");
        generateValues(allData);
        //generateData();
        mchart.setLineChartData(mdata);
    }

    private void generateData() {
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        int X = 0;
        for (Integer value : table.values()) {
            pointValues.add(new PointValue(X, value));
            X++;
        }
        Line line = new Line(pointValues);
        line.setColor(ChartUtils.COLORS[0]).setCubic(true);
        line.setShape(ValueShape.CIRCLE);
        line.setPointColor(ChartUtils.COLORS[1]);
        lines.add(line);
        mdata.setLines(lines);
    }

    private void generateValues(List<CostBean> allData) {
        if (allData != null) {
            for (int i = 0; i < allData.size(); i++) {
                CostBean costBean = allData.get(i);
                String costDate = costBean.costDate;
                int costMoney = Integer.parseInt(costBean.costMoney);

                if (!table.containsKey(costDate)) {
                    table.put(costDate, costMoney);
                } else {
                    int originMoney = table.get(costMoney);
                    table.put(costDate, originMoney + costMoney);
                }
            }
        }
    }
}
ChartActivity
<?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:padding="10dp"
    android:layout_height="match_parent">

    <lecho.lib.hellocharts.view.LineChartView
        android:id="@+id/chart"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
ChartLayout

三、心得体会

图表的添加是在视频上跟着老师做的,但是视频可能突然出了一些问题终止了,然后我也不知道怎么弄,上网搜索因为使用方法不同,思路跟不上,心态爆炸。可能是自己加入到其中的思考不够多,明天继续!

 

猜你喜欢

转载自www.cnblogs.com/suanai/p/12296371.html