第83篇 Android Studio实现油耗记录App加油耗曲线(三) 活动类修改
1.添加变量
保存数据,以及用于其他改变。
List<String> allOilConsumption;
List<String> allSpent;
List<String> allOilCount;
double mallAverageOil;
double mallSpent;
double mallOilCount;
2.init()函数内修改
初始化变量时设定坐标,以及传值。
lineView= findViewById(R.id.lineView);
List<String> yList=new ArrayList<>();
yList.add("1.50");
yList.add("1.00");
yList.add("0.50");
yList.add("0.00");
List<String> xList = new ArrayList<>();
for(int i = 0;i < 16;i++) xList.add(String.valueOf(i));
lineView.setMallAverageOil(mallAverageOil);
lineView.setMallSpent(mallSpent);
lineView.setMallOilCount(mallOilCount);
lineView.setViewData(yList,xList,allOilConsumption);
3.initListData()函数修改
初始化变量。 进行相关计算以及存储。 进行最后的计算。
4.添加一个getValue函数
因为存储的数据带单位(52元),所以要把单位去掉。
public String getValue(String string) {
StringBuilder amt = new StringBuilder();
for(int k = 0;k < string.length();k++) {
if(string.charAt(k) == '.' || (string.charAt(k) > 47 && string.charAt(k) < 58)) {
amt.append(string.charAt(k));
}
}
return amt.toString();
}
5.点击按钮时的修改
很长,看代码吧。
package com.example.listadapter;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,MyDialog.OnCenterItemClickListener{
private MyAdapter myAdapter;
int position;
int dataChange;
LineView lineView;
MyDialog dialog;
List<String> allOilConsumption;
List<String> allSpent;
List<String> allOilCount;
double mallAverageOil;
double mallSpent;
double mallOilCount;
boolean isAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListData();
init();
}
@SuppressLint("ApplySharedPref")
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if(dataChange != 0){
LinkedList<Data> myData = myAdapter.getMyData();
try {
FileOutputStream fileOutputStream = this.openFileOutput("amount.txt", Context.MODE_PRIVATE);
fileOutputStream.close();
fileOutputStream = this.openFileOutput("mileage.txt", Context.MODE_PRIVATE);
fileOutputStream.close();
fileOutputStream = this.openFileOutput("unitPrice.txt", Context.MODE_PRIVATE);
fileOutputStream.close();
fileOutputStream = this.openFileOutput("oilConsumption.txt", Context.MODE_PRIVATE);
fileOutputStream.close();
fileOutputStream = this.openFileOutput("date.txt", Context.MODE_PRIVATE);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if(myData.size() != 0){
for(int i = 0;i < myData.size();i++){
FileHelper.write(this,"amount",
myData.get(i).getAmount() + "\n");
FileHelper.write(this,"mileage",
myData.get(i).getMileage() + "\n");
FileHelper.write(this,"unitPrice",
myData.get(i).getUnitPrice() + "\n");
FileHelper.write(this,"oilConsumption",
myData.get(i).getOilConsumption() + "\n");
FileHelper.write(this,"date",
myData.get(i).getDateTime() + "\n");
}
}
}
}
private void init(){
Button btn_add_one = findViewById(R.id.btn_add_one);
Button btn_delete_one = findViewById(R.id.btn_delete_one);
Button btn_delete_all = findViewById(R.id.btn_delete_all);
Button btn_up_one = findViewById(R.id.btn_up_one);
btn_add_one.setOnClickListener(this);
btn_delete_one.setOnClickListener(this);
btn_delete_all.setOnClickListener(this);
btn_up_one.setOnClickListener(this);
if(myAdapter == null){
myAdapter = new MyAdapter(new LinkedList<>(),this);
}
ListView list_one = findViewById(R.id.list_view);
list_one.setAdapter(myAdapter);
list_one.setOnItemClickListener(this::onItemClick);
dialog =new MyDialog(this,R.layout.data_input_layout,
new int[]{R.id.btn_save,R.id.btn_cancel});
dialog.setOnCenterItemClickListener(this);
lineView= findViewById(R.id.lineView);
List<String> yList=new ArrayList<>();
yList.add("1.50");
yList.add("1.00");
yList.add("0.50");
yList.add("0.00");
List<String> xList = new ArrayList<>();
for(int i = 0;i < 16;i++) xList.add(String.valueOf(i));
lineView.setMallAverageOil(mallAverageOil);
lineView.setMallSpent(mallSpent);
lineView.setMallOilCount(mallOilCount);
lineView.setViewData(yList,xList,allOilConsumption);
}
@SuppressLint("DefaultLocale")
public void initListData(){
String[] amount = FileHelper.read(this,"amount");
String[] mileage = FileHelper.read(this,"mileage");
String[] unitPrice = FileHelper.read(this,"unitPrice");
String[] oilConsumption = FileHelper.read(this,"oilConsumption");
String[] date = FileHelper.read(this,"date");
allOilConsumption = new ArrayList<>();
allSpent = new ArrayList<>();
allOilCount = new ArrayList<>();
mallAverageOil = 0;
mallSpent = 0;
mallOilCount = 0;
LinkedList<Data> myData = new LinkedList<>();
for(int i = 0;i < amount.length;i++) {
Data data = new Data();
data.setImage(R.drawable.image);
data.setAmount(amount[i]);
data.setMileage(mileage[i]);
data.setUnitPrice(unitPrice[i]);
data.setOilConsumption(oilConsumption[i]);
data.setDateTime(date[i]);
myData.add(data);
allOilConsumption.add(getValue(oilConsumption[i]));
double oil = Double.parseDouble(getValue(oilConsumption[i]));
double mil = Double.parseDouble(getValue(mileage[i]));
String spent = String.format("%.2f", Double.parseDouble(getValue(amount[i])));
mallAverageOil += oil;
mallSpent += Double.parseDouble(getValue(amount[i]));
String oilCount = String.format("%.2f",oil * mil);
mallOilCount += oil * mil;
allOilCount.add(oilCount);
allSpent.add(spent);
Log.d("金额:",getValue(amount[i]));
}
myAdapter = new MyAdapter(myData,this);
if (amount.length != 0) {
mallAverageOil /= amount.length;
}
}
public String getValue(String string) {
StringBuilder amt = new StringBuilder();
for(int k = 0;k < string.length();k++) {
if(string.charAt(k) == '.' || (string.charAt(k) > 47 && string.charAt(k) < 58)) {
amt.append(string.charAt(k));
}
}
return amt.toString();
}
private void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
position = i;
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add_one:
isAdd = true;
this.dialog =new MyDialog(this,R.layout.data_input_layout,
new int[]{R.id.btn_save,R.id.btn_cancel});
this.dialog.setOnCenterItemClickListener(this);
this.dialog.show();
break;
case R.id.btn_delete_one:
myAdapter.remove(position);
if(!allOilConsumption.isEmpty() && position < allOilConsumption.size()) {
allOilConsumption.remove(position);
mallSpent -= Double.parseDouble(allSpent.get(position));
if(mallOilCount < 0) {
mallOilCount = 0;
}
mallOilCount -= Double.parseDouble(allOilCount.get(position));
if(mallSpent < 0) {
mallSpent = 0;
}
mallAverageOil = 0;
for(int i = 0;i < allOilConsumption.size();i++) {
mallAverageOil += Double.parseDouble(allOilConsumption.get(i));
}
if(!allOilConsumption.isEmpty()) {
mallAverageOil /= allOilConsumption.size();
}
allSpent.remove(position);
allOilCount.remove(position);
lineView.setMallAverageOil(mallAverageOil);
lineView.setMallSpent(mallSpent);
lineView.setMallOilCount(mallOilCount);
lineView.setMallOilConsumption(allOilConsumption);
}
Toast.makeText(this, "如果没有删除成功,请再次选择删除项!", Toast.LENGTH_SHORT).show();
dataChange++;
break;
case R.id.btn_delete_all:
myAdapter.clearAll();
if(!allOilConsumption.isEmpty()) {
mallSpent = 0;
mallOilCount = 0;
mallAverageOil = 0;
lineView.setMallAverageOil(mallAverageOil);
lineView.setMallSpent(mallSpent);
lineView.setMallOilCount(mallOilCount);
allSpent.clear();
allOilCount.clear();
allOilConsumption.clear();
}
lineView.setMallOilConsumption(allOilConsumption);
dataChange++;
break;
case R.id.btn_up_one:
isAdd = false;
upData();
break;
default:
}
}
@SuppressLint({"NonConstantResourceId", "DefaultLocale"})
@Override
public void OnCenterItemClick(MyDialog dialog, View view) {
switch (view.getId()){
case R.id.btn_save:
EditText edit_amount = dialog.findViewById(R.id.edit_amount);
EditText edit_mileage = dialog.findViewById(R.id.edit_mileage);
EditText edit_unitPrice = dialog.findViewById(R.id.edit_unitPrice);
String amount_unit = this.getResources().getString(R.string.txt_amount_unit);
String mileage_unit = this.getResources().getString(R.string.txt_mileage_unit);
String unitPrice_unit = this.getResources().getString(R.string.txt_unitPrice_unit);
String oilConsumption_unit = this.getResources().getString(R.string.txt_oilConsumption_unit);
String amount = edit_amount.getText().toString() +
this.getResources().getString(R.string.txt_amount_unit);
String mileage = edit_mileage.getText().toString() +
this.getResources().getString(R.string.txt_mileage_unit);
String unitPrice = edit_unitPrice.getText().toString() +
this.getResources().getString(R.string.txt_unitPrice_unit);
if(amount.equals(amount_unit) ||
mileage.equals(mileage_unit) ||
unitPrice.equals(unitPrice_unit) ){
Toast.makeText(this, "请输入数据!", Toast.LENGTH_SHORT).show();
dialog.show();
break;
}
double amountValue = Double.parseDouble(edit_amount.getText().toString());
double unitPriceValue = Double.parseDouble(edit_unitPrice.getText().toString());
double mileageValue = Double.parseDouble(edit_mileage.getText().toString());
if(unitPriceValue == 0 || mileageValue == 0){
Toast.makeText(this, "输入的数据有误!", Toast.LENGTH_SHORT).show();
dialog.show();
break;
}
Date date = new Date();
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE hh:mm:ss");
String edit_date = dateFormat.format(date);
double oilConsumptionValue = (amountValue / unitPriceValue / mileageValue);
@SuppressLint("DefaultLocale") String oilConsumption = String.format("%.2f",oilConsumptionValue) +
oilConsumption_unit;
Data data = new Data();
data.setImage(R.drawable.image);
data.setAmount(amount);
data.setMileage(mileage);
data.setUnitPrice(unitPrice);
data.setOilConsumption(oilConsumption);
data.setDateTime(edit_date);
if(isAdd){
myAdapter.add(data);
allOilConsumption.add(String.format("%.2f",oilConsumptionValue));
mallAverageOil = 0;
for(int i = 0;i < allOilConsumption.size();i++) {
mallAverageOil += Double.parseDouble(allOilConsumption.get(i));
}
mallAverageOil /= allOilConsumption.size();
allSpent.add(String.format("%.2f",amountValue));
mallSpent += amountValue;
allOilCount.add(String.format("%.2f",amountValue / unitPriceValue));
mallOilCount += amountValue / unitPriceValue;
}
else{
myAdapter.updateListItem(data,position);
allOilConsumption.set(position , String.format("%.2f",oilConsumptionValue));
mallAverageOil = 0;
for(int i = 0;i < allOilConsumption.size();i++) {
mallAverageOil += Double.parseDouble(allOilConsumption.get(i));
}
mallAverageOil /= allOilConsumption.size();
allSpent.set(position, String.format("%.2f",amountValue));
mallSpent = 0;
for(int i = 0;i < allSpent.size();i++) {
mallSpent += Double.parseDouble(allSpent.get(i));
}
allOilCount.set(position, String.format("%.2f",amountValue / unitPriceValue));
mallOilCount = 0;
for(int i = 0;i < allOilCount.size();i++) {
mallOilCount += Double.parseDouble(allOilCount.get(i));
}
}
dataChange++;
lineView.setMallAverageOil(mallAverageOil);
lineView.setMallSpent(mallSpent);
lineView.setMallOilCount(mallOilCount);
lineView.setMallOilConsumption(allOilConsumption);
break;
case R.id.btn_cancel:
}
}
public void upData(){
LinkedList<Data> myData = myAdapter.getMyData();
if(myData == null ){
return;
}
else if(position >= myData.size() || position < 0){
return;
}
this.dialog.show();
this.dialog.cancel();
EditText edit_amount = this.dialog.findViewById(R.id.edit_amount);
EditText edit_mileage = this.dialog.findViewById(R.id.edit_mileage);
EditText edit_unitPrice = this.dialog.findViewById(R.id.edit_unitPrice);
Data data = myData.get(position);
if(data == null){
return;
}
edit_amount.setText(data.getAmount().replaceAll(this.getResources().
getString(R.string.txt_amount_unit),""));
edit_mileage.setText(data.getMileage().replaceAll(this.getResources().
getString(R.string.txt_mileage_unit),""));
edit_unitPrice.setText(data.getUnitPrice().replaceAll(this.getResources().
getString(R.string.txt_unitPrice_unit),""));
dialog.show();
}
}
|