定义实体类Product, 包含属性:id,name,time,price; 创建Activity,在界面上输入一些Product对象,并将它们保存到SQLite数据库中,同时也可以输入关键字进行检索出相应的Product对象。
时间: 2024-10-21 11:07:52 浏览: 82
首先,我们创建一个名为`Product`的实体类,它包含所需的属性:
```java
public class Product {
private int id;
private String name;
private long time; // 时间通常以毫秒为单位
private double price;
// 构造函数
public Product(int id, String name, long time, double price) {
this.id = id;
this.name = name;
this.time = time;
this.price = price;
}
// getter 和 setter 方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public long getTime() { return time; }
public void setTime(long time) { this.time = time; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
```
接下来,我们需要一个`DatabaseHelper`类来处理SQLite操作,包括添加、查询产品:
```java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
// 数据库表名和字段定义
private static final String TABLE_NAME = "products";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_TIME = "time";
private static final String COLUMN_PRICE = "price";
// 索引和创建表的SQL语句
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_NAME + " TEXT NOT NULL," +
COLUMN_TIME + " INTEGER NOT NULL," +
COLUMN_PRICE + " REAL NOT NULL)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
// 添加产品到数据库
public void addProduct(Product product) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NAME, product.getName());
contentValues.put(COLUMN_TIME, product.getTime());
contentValues.put(COLUMN_PRICE, product.getPrice());
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
// 根据关键字检索产品
public Cursor searchProducts(String keyword) {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE name LIKE ?";
return db.rawQuery(query, new String[]{ "%" + keyword + "%" });
}
}
```
在`Activity`中,你可以通过`EditText`获取用户输入,然后实例化`Product`对象并保存到数据库,以及提供搜索功能:
```java
// 获取用户输入并保存
EditText inputName = findViewById(R.id.input_name);
EditText inputTime = findViewById(R.id.input_time);
EditText inputPrice = findViewById(R.id.input_price);
Button saveBtn = findViewById(R.id.save_button);
saveBtn.setOnClickListener(view -> {
String name = inputName.getText().toString();
long time = Long.parseLong(inputTime.getText().toString());
double price = Double.parseDouble(inputPrice.getText().toString());
Product product = new Product(0, name, time, price); // 假设id暂未设置
databaseHelper.addProduct(product);
});
// 搜索产品
EditText searchKeyword = findViewById(R.id.search_keyword);
Button searchBtn = findViewById(R.id.search_button);
searchBtn.setOnClickListener(view -> {
String keyword = searchKeyword.getText().toString();
Cursor cursor = databaseHelper.searchProducts(keyword);
// 处理查询结果
});
```
阅读全文