android用sqllite数据库实现诗词的增删改查功能,写一个详细的简单示例,写清文件名

时间: 2023-07-16 22:15:21 浏览: 41
好的,以下是一个简单的 Android 应用程序,使用 SQLite 数据库实现诗词的增删改查功能,包括以下几个文件: 1. `Poem.java`:定义了 `Poem` 类,表示一首诗词。 ```java public class Poem { private long id; private String title; private String author; private String content; public Poem() {} public Poem(String title, String author, String content) { this.title = title; this.author = author; this.content = content; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } ``` 2. `PoemDatabaseHelper.java`:定义了 `PoemDatabaseHelper` 类,继承自 `SQLiteOpenHelper`,用于创建和更新数据库。 ```java public class PoemDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "poems.db"; private static final int DATABASE_VERSION = 1; public PoemDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE poems (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, content TEXT);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS poems;"); onCreate(db); } } ``` 3. `PoemDataSource.java`:定义了 `PoemDataSource` 类,用于操作数据库,包括增删改查功能。 ```java public class PoemDataSource { private SQLiteDatabase database; private PoemDatabaseHelper dbHelper; private String[] allColumns = { "_id", "title", "author", "content" }; public PoemDataSource(Context context) { dbHelper = new PoemDatabaseHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } public Poem createPoem(String title, String author, String content) { ContentValues values = new ContentValues(); values.put("title", title); values.put("author", author); values.put("content", content); long insertId = database.insert("poems", null, values); Cursor cursor = database.query("poems", allColumns, "_id = " + insertId, null, null, null, null); cursor.moveToFirst(); Poem newPoem = cursorToPoem(cursor); cursor.close(); return newPoem; } public void deletePoem(Poem poem) { long id = poem.getId(); database.delete("poems", "_id = " + id, null); } public void updatePoem(Poem poem) { long id = poem.getId(); ContentValues values = new ContentValues(); values.put("title", poem.getTitle()); values.put("author", poem.getAuthor()); values.put("content", poem.getContent()); database.update("poems", values, "_id = " + id, null); } public List<Poem> getAllPoems() { List<Poem> poems = new ArrayList<Poem>(); Cursor cursor = database.query("poems", allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Poem poem = cursorToPoem(cursor); poems.add(poem); cursor.moveToNext(); } cursor.close(); return poems; } private Poem cursorToPoem(Cursor cursor) { Poem poem = new Poem(); poem.setId(cursor.getLong(0)); poem.setTitle(cursor.getString(1)); poem.setAuthor(cursor.getString(2)); poem.setContent(cursor.getString(3)); return poem; } } ``` 4. `MainActivity.java`:定义了 `MainActivity` 类,是 Android 应用程序的主界面,用于展示诗词列表、添加诗词等操作。 ```java public class MainActivity extends AppCompatActivity { private PoemDataSource dataSource; private List<Poem> poems = new ArrayList<>(); private PoemAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataSource = new PoemDataSource(this); dataSource.open(); ListView listView = findViewById(R.id.list_view); adapter = new PoemAdapter(this, poems); listView.setAdapter(adapter); Button addButton = findViewById(R.id.add_button); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showAddDialog(); } }); loadPoems(); } @Override protected void onResume() { super.onResume(); dataSource.open(); } @Override protected void onPause() { super.onPause(); dataSource.close(); } private void loadPoems() { poems.clear(); poems.addAll(dataSource.getAllPoems()); adapter.notifyDataSetChanged(); } private void showAddDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Add Poem"); View view = getLayoutInflater().inflate(R.layout.dialog_add_poem, null); final EditText titleEditText = view.findViewById(R.id.title_edit_text); final EditText authorEditText = view.findViewById(R.id.author_edit_text); final EditText contentEditText = view.findViewById(R.id.content_edit_text); builder.setView(view); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String title = titleEditText.getText().toString(); String author = authorEditText.getText().toString(); String content = contentEditText.getText().toString(); dataSource.createPoem(title, author, content); loadPoems(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } private class PoemAdapter extends ArrayAdapter<Poem> { public PoemAdapter(Context context, List<Poem> poems) { super(context, 0, poems); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.list_item_poem, parent, false); } Poem poem = getItem(position); TextView titleTextView = convertView.findViewById(R.id.title_text_view); TextView authorTextView = convertView.findViewById(R.id.author_text_view); TextView contentTextView = convertView.findViewById(R.id.content_text_view); titleTextView.setText(poem.getTitle()); authorTextView.setText(poem.getAuthor()); contentTextView.setText(poem.getContent()); return convertView; } } } ``` 5. `activity_main.xml`:定义了 `MainActivity` 类的布局。 ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider="@null"/> <Button android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add"/> </LinearLayout> ``` 6. `dialog_add_poem.xml`:定义了添加诗词对话框的布局。 ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/title_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Title"/> <EditText android:id="@+id/author_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Author"/> <EditText android:id="@+id/content_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Content"/> </LinearLayout> ``` 以上是一个简单的 Android 应用程序,使用 SQLite 数据库实现诗词的增删改查功能,文件名清单如下: - `Poem.java` - `PoemDatabaseHelper.java` - `PoemDataSource.java` - `MainActivity.java` - `activity_main.xml` - `dialog_add_poem.xml`

相关推荐

最新推荐

recommend-type

C#操作SQLite数据库之读写数据库的方法

主要介绍了C#操作SQLite数据库之读写数据库的方法,简单分析了C#针对SQLite数据库的读写及显示等操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

C#操作SQLite数据库方法小结(创建,连接,插入,查询,删除等)

主要介绍了C#操作SQLite数据库方法,包括针对SQLite数据库的创建,连接,插入,查询,删除等操作,并提供了一个SQLite的封装类,需要的朋友可以参考下
recommend-type

android中SQLite数据库中用insert同时插入多条记录的方法和效率比较

android中SQLite数据库中用insert同时插入多条记录的方法和效率比较
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这