IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 第83篇 Android Studio实现油耗记录App加油耗曲线(三) 活动类修改 -> 正文阅读

[移动开发]第83篇 Android Studio实现油耗记录App加油耗曲线(三) 活动类修改

第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();
    }
}
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-11-30 15:43:53  更:2021-11-30 15:45:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:52:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码