点击右上角可以实现将花费以折线图的形式显示出来。同时将同一天的花费自动计算。暂时还没有加x,y轴
ChartsActivity.java:
package com.example.family; 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.ChartData; 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.Chart; import lecho.lib.hellocharts.view.LineChartView; public class ChartsActivity 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=(LineChartView) findViewById(R.id.chart); mData=new LineChartData(); List<CostBean> allDate= (List<CostBean>) getIntent().getSerializableExtra("cost_list"); generateValues(allDate); generateData(); } private void generateData() { List<Line> lines=new ArrayList<>(); List<PointValue> values=new ArrayList<>(); int indexX=0; for (Integer value:table.values()){ values.add(new PointValue(indexX,value)); indexX++; } Line line=new Line(values); line.setColor(ChartUtils.COLORS[0]); line.setShape(ValueShape.CIRCLE); line.setPointColor(ChartUtils.COLORS[1]); lines.add(line); mData =new LineChartData(lines); mData.setLines(lines); mChart.setLineChartData(mData); } private void generateValues(List<CostBean> allDate) { if(allDate!=null){ for(int i=0;i<allDate.size();i++){ CostBean costBean=allDate.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(costDate); table.put(costDate,originMoney+costMoney); } } } } }
MainActivity.java:
package com.example.family; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ListView; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<CostBean> mCostBeanList; private DatabaseHelper mDatabaseHelper; private CostListAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); mDatabaseHelper=new DatabaseHelper(this); mCostBeanList=new ArrayList<>(); ListView costList=(ListView)findViewById(R.id.lv_main); initCostData(); mAdapter =new CostListAdapter(this,mCostBeanList); costList.setAdapter(mAdapter); FloatingActionButton btn_add = findViewById(R.id.btn_add); btn_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); LayoutInflater inflater=LayoutInflater.from(MainActivity.this); View viewDialog=inflater.inflate(R.layout.new_cost_data,null); final EditText title=(EditText)viewDialog.findViewById(R.id.et_cost_title); final EditText money=(EditText)viewDialog.findViewById(R.id.et_cost_money); final DatePicker date=(DatePicker) viewDialog.findViewById(R.id.dp_cost_date); builder.setView(viewDialog); builder.setTitle("New Cost"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { CostBean costBean=new CostBean(); costBean.costTitle=title.getText().toString(); costBean.costMoney=money.getText().toString(); costBean.costDate=date.getYear() + "-" + (date.getMonth()+1) + "-" + date.getDayOfMonth(); mDatabaseHelper.insertCost(costBean); mCostBeanList.add(costBean); mAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("Cancle",null); builder.create().show(); } }); private void initCostData() { Cursor cursor=mDatabaseHelper.getAllCostData(); if(cursor!=null){ while(cursor.moveToNext()){ CostBean costBean=new CostBean(); costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title")); costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date")); costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money")); mCostBeanList.add(costBean); } cursor.close(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_chart) { Intent intent=new Intent(MainActivity.this,ChartsActivity.class); intent.putExtra("cost_list", (Serializable) mCostBeanList); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } }
chart_view.xml:
<?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:padding="10dp"> <lecho.lib.hellocharts.view.LineChartView android:id="@+id/chart" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" /> </LinearLayout>
注意:在AndroidMainifest.xml中添加
<activity android:name=".ChartsActivity" android:label="Chart"/>