用android的sqllite数据库实现古诗词的增、删、查、改功能,写清楚布局文件、Java文件
时间: 2023-07-10 10:03:51 浏览: 99
好的,以下是一个简单的古诗词增删改查的 Android 应用示例,使用 SQLite 数据库进行存储管理。
首先,我们需要创建一个布局文件 `activity_main.xml`,包含一个用于显示列表的 ListView 和一些用于添加新诗词的 EditText 和 Button。
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/input_layout"/>
<RelativeLayout
android:id="@+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#eeeeee">
<EditText
android:id="@+id/title_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入标题"/>
<EditText
android:id="@+id/author_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title_input"
android:hint="输入作者"/>
<EditText
android:id="@+id/content_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/author_input"
android:hint="输入内容"/>
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/content_input"
android:layout_alignParentEnd="true"
android:text="添加"/>
</RelativeLayout>
</RelativeLayout>
```
接下来,我们需要创建一个 Java 类 `Poem`,用于表示一首诗词的数据结构。
```java
public class Poem {
public int id;
public String title;
public String author;
public String content;
public Poem(int id, String title, String author, String content) {
this.id = id;
this.title = title;
this.author = author;
this.content = content;
}
}
```
然后,我们需要创建一个 SQLite 数据库帮助类 `PoemDatabaseHelper`,用于创建和管理数据库表。
```java
public class PoemDatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Poem.db";
public PoemDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE poem(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, content TEXT)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS poem");
onCreate(db);
}
}
```
现在,我们可以创建一个主 Activity 类 `MainActivity`,处理所有的 UI 交互和数据库操作。
```java
public class MainActivity extends AppCompatActivity {
private ListView listView;
private EditText titleInput;
private EditText authorInput;
private EditText contentInput;
private Button addButton;
private PoemDatabaseHelper dbHelper;
private SQLiteDatabase db;
private List<Poem> poemList = new ArrayList<>();
private PoemAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
titleInput = findViewById(R.id.title_input);
authorInput = findViewById(R.id.author_input);
contentInput = findViewById(R.id.content_input);
addButton = findViewById(R.id.add_button);
dbHelper = new PoemDatabaseHelper(this);
db = dbHelper.getWritableDatabase();
adapter = new PoemAdapter(this, poemList);
listView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String title = titleInput.getText().toString().trim();
String author = authorInput.getText().toString().trim();
String content = contentInput.getText().toString().trim();
if (!title.isEmpty() && !author.isEmpty() && !content.isEmpty()) {
ContentValues values = new ContentValues();
values.put("title", title);
values.put("author", author);
values.put("content", content);
db.insert("poem", null, values);
refreshList();
titleInput.setText("");
authorInput.setText("");
contentInput.setText("");
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Poem poem = poemList.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(poem.title)
.setMessage(poem.content)
.setNegativeButton("删除", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
db.delete("poem", "id=?", new String[]{String.valueOf(poem.id)});
refreshList();
}
})
.setPositiveButton("编辑", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, EditActivity.class);
intent.putExtra("id", poem.id);
intent.putExtra("title", poem.title);
intent.putExtra("author", poem.author);
intent.putExtra("content", poem.content);
startActivityForResult(intent, 1);
}
})
.show();
}
});
refreshList();
}
protected void onDestroy() {
super.onDestroy();
db.close();
dbHelper.close();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
int id = data.getIntExtra("id", 0);
String title = data.getStringExtra("title");
String author = data.getStringExtra("author");
String content = data.getStringExtra("content");
ContentValues values = new ContentValues();
values.put("title", title);
values.put("author", author);
values.put("content", content);
db.update("poem", values, "id=?", new String[]{String.valueOf(id)});
refreshList();
}
}
private void refreshList() {
poemList.clear();
Cursor cursor = db.query("poem", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String author = cursor.getString(cursor.getColumnIndex("author"));
String content = cursor.getString(cursor.getColumnIndex("content"));
poemList.add(new Poem(id, title, author, content));
} while (cursor.moveToNext());
}
cursor.close();
adapter.notifyDataSetChanged();
}
private class PoemAdapter extends BaseAdapter {
private Context context;
private List<Poem> poemList;
public PoemAdapter(Context context, List<Poem> poemList) {
this.context = context;
this.poemList = poemList;
}
public int getCount() {
return poemList.size();
}
public Object getItem(int position) {
return poemList.get(position);
}
public long getItemId(int position) {
return poemList.get(position).id;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_poem, parent, false);
}
TextView titleView = convertView.findViewById(R.id.title_text);
TextView authorView = convertView.findViewById(R.id.author_text);
TextView contentView = convertView.findViewById(R.id.content_text);
Poem poem = poemList.get(position);
titleView.setText(poem.title);
authorView.setText(poem.author);
contentView.setText(poem.content);
return convertView;
}
}
}
```
最后,我们需要创建一个编辑 Activity 类 `EditActivity`,用于编辑已有的诗词。
```java
public class EditActivity extends AppCompatActivity {
private EditText titleInput;
private EditText authorInput;
private EditText contentInput;
private Button saveButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
titleInput = findViewById(R.id.title_input);
authorInput = findViewById(R.id.author_input);
contentInput = findViewById(R.id.content_input);
saveButton = findViewById(R.id.save_button);
Bundle extras = getIntent().getExtras();
final int id = extras.getInt("id");
String title = extras.getString("title");
String author = extras.getString("author");
String content = extras.getString("content");
titleInput.setText(title);
authorInput.setText(author);
contentInput.setText(content);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String title = titleInput.getText().toString().trim();
String author = authorInput.getText().toString().trim();
String content = contentInput.getText().toString().trim();
if (!title.isEmpty() && !author.isEmpty() && !content.isEmpty()) {
Intent intent = new Intent();
intent.putExtra("id", id);
intent.putExtra("title", title);
intent.putExtra("author", author);
intent.putExtra("content", content);
setResult(RESULT_OK, intent);
finish();
}
}
});
}
}
```
以上就是一个简单的古诗词增删改查的 Android 应用示例了。请注意,为了简化示例代码,我们将数据库操作和 UI 代码都放在了主 Activity 类中,实际开发中应该将它们分别放在不同的类中。同时,我们也没有添加任何异常处理和用户输入校验,这些也是实际开发中必须考虑的问题。
阅读全文