使用sharedpreferences 1、创建名诗词数据表,字段主要包括:id、题名、作者姓名、全文。 2、实现添加、查询、修改、删除诗词信息的功能。
时间: 2023-11-08 22:56:31 浏览: 82
首先,我们需要创建一个Poetry类来存储诗词信息,包括id、题名、作者姓名和全文:
```java
public class Poetry {
private int id;
private String title;
private String author;
private String content;
public Poetry(int id, String title, String author, String content) {
this.id = id;
this.title = title;
this.author = author;
this.content = content;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getContent() {
return content;
}
}
```
接下来,我们需要创建一个PoetryDatabase类来管理诗词数据表,包括添加、查询、修改、删除等操作:
```java
public class PoetryDatabase {
private static final String PREF_NAME = "poetry_database";
private static final String KEY_NEXT_ID = "next_id";
private SharedPreferences preferences;
private int nextId;
public PoetryDatabase(Context context) {
preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
nextId = preferences.getInt(KEY_NEXT_ID, 1);
}
public List<Poetry> getAllPoetries() {
List<Poetry> poetries = new ArrayList<>();
Map<String, ?> allEntries = preferences.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
String key = entry.getKey();
if (key.startsWith("poetry_")) {
String[] parts = key.split("_");
int id = Integer.parseInt(parts[1]);
String[] values = ((String) entry.getValue()).split(",");
String title = values[0];
String author = values[1];
String content = values[2];
Poetry poetry = new Poetry(id, title, author, content);
poetries.add(poetry);
}
}
return poetries;
}
public Poetry getPoetryById(int id) {
String key = "poetry_" + id;
String value = preferences.getString(key, null);
if (value != null) {
String[] values = value.split(",");
String title = values[0];
String author = values[1];
String content = values[2];
return new Poetry(id, title, author, content);
} else {
return null;
}
}
public void addPoetry(Poetry poetry) {
String key = "poetry_" + poetry.getId();
String value = poetry.getTitle() + "," + poetry.getAuthor() + "," + poetry.getContent();
preferences.edit().putString(key, value).apply();
nextId++;
preferences.edit().putInt(KEY_NEXT_ID, nextId).apply();
}
public void updatePoetry(Poetry poetry) {
String key = "poetry_" + poetry.getId();
String value = poetry.getTitle() + "," + poetry.getAuthor() + "," + poetry.getContent();
preferences.edit().putString(key, value).apply();
}
public void deletePoetry(Poetry poetry) {
String key = "poetry_" + poetry.getId();
preferences.edit().remove(key).apply();
}
}
```
在PoetryDatabase中,我们使用SharedPreferences来存储诗词数据表,并提供了一些操作,如getAllPoetries、getPoetryById、addPoetry、updatePoetry和deletePoetry等。
具体使用方法如下:
```java
// 创建PoetryDatabase对象
PoetryDatabase db = new PoetryDatabase(context);
// 添加一首诗词
Poetry poetry = new Poetry(1, "静夜思", "李白", "床前明月光,疑是地上霜。举头望明月,低头思故乡。");
db.addPoetry(poetry);
// 查询所有诗词
List<Poetry> allPoetries = db.getAllPoetries();
// 查询指定id的诗词
Poetry poetry = db.getPoetryById(1);
// 修改诗词
poetry.setTitle("将进酒");
db.updatePoetry(poetry);
// 删除诗词
db.deletePoetry(poetry);
```
阅读全文