Android家庭记账本开发第八天:修改和删除功能的设计

之前两篇我们讲了增加和查询功能,但是这两个功能都是通过设置点击函数实现跳转逻辑的,现在我们要实现修改和删除功能就需要获取到当前的数据项,那么又该怎么做呢

在讲解之前的适配器设置的时候已经为每个列表项设置了监听器,并且在主页面逻辑当中为点击函数进行了重写,现在我们来看修改删除页面是怎样处理传递的数据的,这里先附上点击监听器函数的代码:

@Override
    public void onItemClick(int position) {
        // 处理点击事件的逻辑
        Log.d("Main", "jump");
        costList item = list.get(position);
        Intent intent = new Intent(MainActivity.this, upgrade_cost.class);
        intent.putExtra("item_id", item.get_id());
        startActivityForResult(intent, 1);
    }

可以看到我们将点击项的id作为传输数据进行传递,当然这里也可以将position作为参数进行传递,以id进行查询会简单一点,这里给出修改删除页面的逻辑代码:

package com.example.myapplication3;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class upgrade_cost extends AppCompatActivity {
    private DBHelper helper;
    private EditText et_cost_title;
    private EditText et_cost_money;
    private DatePicker dp_cost_date;
    List<costList> mList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrade_cost);
        initView();
    }

    private void initView() {
        helper = new DBHelper(upgrade_cost.this);

        et_cost_title = findViewById(R.id.et_cost_title);
        et_cost_money = findViewById(R.id.et_cost_money);
        dp_cost_date = findViewById(R.id.dp_cost_date);

        // 获取传递的数据
        int itemId = getIntent().getIntExtra("item_id", -1);
        Log.d("DBHelper", "Item id: " + itemId);
        costList item = helper.getCostListById(itemId);
        if(item != null){
            String title = item.getTitle();
            String money = item.getMoney();
            String date = item.getDate();

            // 将数据填充到对应的 EditText 和 DatePicker 控件中
            et_cost_title.setText(title);
            et_cost_money.setText(money);

            // 将日期字符串转换为年月日三个部分
            String[] dateParts = date.split("-");
            int year = Integer.parseInt(dateParts[0]);
            int month = Integer.parseInt(dateParts[1]) - 1; // 月份需要减去 1
            int day = Integer.parseInt(dateParts[2]);

            // 设置 DatePicker 的初始日期
            dp_cost_date.init(year, month, day, null);

        }

    }


    public void goback(View view){
        finish();
    }
    public void upgradeButton(View view) {
        int itemId = getIntent().getIntExtra("item_id", -1);
        String titleStr = et_cost_title.getText().toString().trim();
        String moneyStr = et_cost_money.getText().toString().trim();
        String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-"
                + dp_cost_date.getDayOfMonth();//这里getMonth会比当前月份少一个月,所以要+1
        if ("".equals(moneyStr)) {//可以不填写Title但是不能不填金额
            Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            SQLiteDatabase db = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("Title", titleStr);
            values.put("Money", moneyStr);
            values.put("Date", dateStr);
            int rowsAffected = db.update("account", values, "_id = ?", new String[] {String.valueOf(itemId)});
            if (rowsAffected > 0) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                setResult(1);
                finish();
            } else {
                Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
            }

            setResult(1);
            finish();
        }

    }
    public void deleteButton(View view){
    // 获取要删除的数据项的行ID,假设从传递的 Intent 中获取
        int itemId = getIntent().getIntExtra("item_id", -1);
        // 获取可写的数据库对象
        SQLiteDatabase db = helper.getWritableDatabase();

        // 执行删除操作
        int rowsAffected = db.delete("account", "_id = ?", new String[] {String.valueOf(itemId)});
        // 删除操作成功后,rowsAffected 的值表示受影响的行数
        if (rowsAffected > 0) {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            setResult(1);
            finish(); // 删除成功后关闭当前 Activity
        } else {
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        }

        // 关闭数据库连接
        db.close();

    }
}

关于数据处理的方面和新增操作类似,既然我们要对已有的数据进行修改,就要将点击的数据项填充到输入框当中,我们通过int itemId = getIntent().getIntExtra("item_id", -1);将点击的数据项id提取出来,并通过在数据库中设置的利用id查询数据的方法将数据项获取,并填充到对应的输入框当中,最后用户点击修改按钮通过数据库的update函数进行修改。

删除就只需要将传入的对应id数据项利用delete函数进行删除即可。
下面是布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    >


    <EditText
        android:id="@+id/et_cost_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:hint="Cost Title"
        android:textColor="#ffbd27"
        />

    <EditText
        android:id="@+id/et_cost_money"
        android:inputType="number|numberDecimal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:hint="Cost Money"
        android:textColor="#ffbd27"
        />

    <DatePicker
        android:id="@+id/dp_cost_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:datePickerMode="spinner"
        android:calendarViewShown="false"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="10dp"
            android:background="#00BCD4"
            android:onClick="upgradeButton"
            android:text="修改"
            android:textColor="#333333"
            android:textSize="20dp" />

        <Button
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="20dp"
            android:background="#00BCD4"
            android:onClick="deleteButton"
            android:text="删除"
            android:textColor="#333333"
            android:textSize="20dp" />

    </LinearLayout>
    <Button
        android:onClick="goback"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="20dp"
        android:background="#00BCD4"
        android:text="返回"
        android:textColor="#333333"
        android:textSize="20dp" />
</LinearLayout>
posted @   起名字真难_qmz  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示