<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品销售系统"
android:gravity="center"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<Button
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#64B6DC"
android:layout_weight="1"
android:text="名称"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp" />
<Button
android:id="@+id/et_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#64B6DC"
android:layout_weight="1"
android:text="价格"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp" />
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="49dp"
android:layout_marginTop="1dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#FFEB3B"
android:text="查询"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/et_name1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint=":输入名称"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
<EditText
android:id="@+id/et_price1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint=":输入价格"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#E47C21"
android:layout_weight="1"
android:text="删除"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="@+id/et_size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#68BBE1"
android:layout_weight="1"
android:text="大小"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
<Button
android:id="@+id/et_amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#64B6DC"
android:layout_weight="1"
android:text="数量"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#64DCAC"
android:layout_weight="1"
android:text="增加"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/et_size1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint=":输入大小"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
<EditText
android:id="@+id/et_amount1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint=":输入数量"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
<Button
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#DCD664"
android:layout_weight="1"
android:text="修改"
android:textSize="20sp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="列表"
android:textSize="18sp"
android:gravity="center"
android:padding="10dp"
android:background="#88E8E5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:longClickable="true"
android:layout_centerVertical="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="编号"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="名称"
android:layout_weight="1"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="单价"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="数量"
android:layout_weight="1"
android:textSize="20sp"/>
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="@color/teal_200"
android:textSize="20sp" />
</LinearLayout>
package com.example.yuyu;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MyHelper myHelper;
private EditText mEtName;
private EditText mEtPrice;
private EditText mEtAmount;
private EditText mEtSize;
private TextView mTvShow;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelete;
private long lastInsertedId = -1; // 用于跟踪最后插入的ID
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
private void init() {
mEtName = findViewById(R.id.et_name1);
mEtPrice = findViewById(R.id.et_price1);
mEtAmount = findViewById(R.id.et_amount1);
mEtSize = findViewById(R.id.et_size1);
mTvShow = findViewById(R.id.tv_show);
mBtnAdd = findViewById(R.id.btn_add);
mBtnQuery = findViewById(R.id.btn_query);
mBtnUpdate = findViewById(R.id.btn_update);
mBtnDelete = findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name, price, size, amount;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()) {
case R.id.btn_add:
name = mEtName.getText().toString();
price = mEtPrice.getText().toString();
size = mEtSize.getText().toString();
amount = mEtAmount.getText().toString();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name", name);
values.put("price", price);
values.put("size", size);
values.put("amount", amount);
lastInsertedId = db.insert("information", null, values);
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_query:
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information", null, null, null, null, null, null);
mTvShow.setText("");
if (cursor.moveToFirst()) {
do {
mTvShow.append("Name:" + cursor.getString(1) + "; price:" + cursor.getString(2)
+ "; size:" + cursor.getString(3) + "; amount:" + cursor.getString(4) + "\n");
} while (cursor.moveToNext());
} else {
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
}
cursor.close();
db.close();
break;
case R.id.btn_update:
updateLastInserted();
break;
case R.id.btn_delete:
deleteLastInserted();
break;
}
}
private void updateLastInserted() {
if (lastInsertedId == -1) {
Toast.makeText(this, "没有可更新的记录", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = myHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", mEtPrice.getText().toString());
values.put("size", mEtSize.getText().toString());
values.put("amount", mEtAmount.getText().toString());
int rowsAffected = db.update("information", values, "_id=?", new String[]{String.valueOf(lastInsertedId)});
if (rowsAffected > 0) {
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
}
db.close();
}
private void deleteLastInserted() {
if (lastInsertedId == -1) {
Toast.makeText(this, "没有可删除的记录", Toast.LENGTH_SHORT).show();
return;
}
SQLiteDatabase db = myHelper.getWritableDatabase();
int rowsAffected = db.delete("information", "_id=?", new String[]{String.valueOf(lastInsertedId)});
if (rowsAffected > 0) {
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
lastInsertedId = -1;
} else {
Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
}
db.close();
}
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "yu.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name VARCHAR(20), price VARCHAR(20), size VARCHAR(20), amount VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 处理数据库升级逻辑(如果需要)
}
}
}