You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Activity中通过按钮为SQLite用户表指定行更新字段值

实现步骤

1. 给DBHelper类新增更新booking字段的方法

在DBHelper类中添加如下方法,用于更新指定用户的booking字段值:

// 添加到DBHelper类的末尾即可
public Boolean updateUserBooking(String username, int bookingId){
    SQLiteDatabase MyDB = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(BOOKING, bookingId);
    // 仅更新用户名匹配的行,用占位符避免SQL注入风险
    int affectedRows = MyDB.update(USERS, contentValues, USERNAME + " = ?", new String[]{username});
    // 影响行数大于0说明更新成功
    return affectedRows > 0;
}

2. 给BookingActivity布局添加按钮

在res/layout/activity_booking.xml中添加确认预订按钮,示例布局代码:

<Button
    android:id="@+id/btn_confirm_booking"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="确认预订"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginBottom="32dp"/>

3. 修改BookingActivity逻辑

需要把局部变量intValue和用户名改成类成员变量,绑定按钮并设置点击事件,完整修改后的代码如下:

public class BookingActivity extends AppCompatActivity {

    TextView textView;
    TextView departure, arrival, duration, price, seats, textView13;
    Button btnConfirmBooking; // 新增按钮成员变量
    DBHelper dbHelper;
    FlightsModel flightsModel;
    int intValue; // 转为成员变量,供点击事件调用
    String userName; // 存储当前用户名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking);

        dbHelper = new DBHelper(this);

        textView = findViewById(R.id.textView);
        departure = findViewById(R.id.departure);
        arrival = findViewById(R.id.arrival);
        duration = findViewById(R.id.duration);
        price = findViewById(R.id.price);
        seats = findViewById(R.id.seats);
        textView13 = findViewById(R.id.textView13);
        btnConfirmBooking = findViewById(R.id.btn_confirm_booking); // 绑定布局中的按钮

        Intent mIntent = getIntent();
        intValue = mIntent.getIntExtra("intVariableName", 0) + 1;
        userName = mIntent.getStringExtra("userName");
        flightsModel = dbHelper.getFlightById(intValue);

        textView.setText(flightsModel.getDestination());
        departure.setText(flightsModel.getDeparture_time());
        arrival.setText(flightsModel.getArrival_time());
        duration.setText(flightsModel.getDuration());
        price.setText("R" + Double.toString(flightsModel.getPrice()));
        seats.setText(Integer.toString(flightsModel.getAvailable_space()));
        textView13.setText(userName);

        // 绑定按钮点击事件
        btnConfirmBooking.setOnClickListener(v -> {
            boolean updateRes = dbHelper.updateUserBooking(userName, intValue);
            if (updateRes) {
                Toast.makeText(BookingActivity.this, "预订成功", Toast.LENGTH_SHORT).show();
                // 可在此处添加跳转逻辑,比如返回上一页
                finish();
            } else {
                Toast.makeText(BookingActivity.this, "预订失败,请重试", Toast.LENGTH_SHORT).show();
            }
        });

        Toast.makeText(BookingActivity.this, flightsModel.getDestination(), Toast.LENGTH_SHORT).show();
    }
}

注意事项

  • 如果之前已经在设备上安装过该应用,需要卸载重装,或者把DBHelper构造方法的最后一个参数版本号从1改成2触发数据库升级,避免旧版本表结构没有booking字段导致报错。
  • 当前逻辑为单预订模式,新的预订会覆盖用户之前存储的booking值,如果需要支持多预订,可以把booking字段改为字符串类型存储多个航班ID。

内容的提问来源于stack exchange,提问作者forsure

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 19:15:07