用android中的sqllite数据库写一个关于古诗的增加、删除,以及按照题目修改和查询功能的笔记本项目,请写一个详细的包括布局文件和java文件的示例

时间: 2024-02-11 22:04:55 浏览: 23
好的,我可以为您提供一个简单的示例。 首先,您需要创建一个包含以下组件的布局文件: 1. 一个EditText用于输入古诗题目 2. 一个EditText用于输入古诗内容 3. 一个添加按钮 4. 一个删除按钮 5. 一个修改按钮 6. 一个查询按钮 7. 一个ListView用于显示所有的古诗列表 接下来,您需要编写一个Java类来处理数据库操作。这个类应该包括以下方法: 1. onCreate()方法用于创建数据库表 2. addPoem()方法用于添加新的古诗 3. deletePoem()方法用于从数据库中删除古诗 4. updatePoem()方法用于修改古诗 5. getAllPoems()方法用于获取所有的古诗列表 6. searchPoem()方法用于根据古诗题目搜索古诗 以下是示例代码: 布局文件 `activity_main.xml`: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/edit_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入古诗题目" /> <EditText android:id="@+id/edit_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入古诗内容" /> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/btn_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" /> <Button android:id="@+id/btn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" /> <ListView android:id="@+id/list_poems" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> ``` Java文件 `MainActivity.java`: ```java public class MainActivity extends AppCompatActivity { private EditText mTitleEditText; private EditText mContentEditText; private Button mAddButton; private Button mDeleteButton; private Button mUpdateButton; private Button mSearchButton; private ListView mPoemsListView; private SQLiteDatabase mDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitleEditText = findViewById(R.id.edit_title); mContentEditText = findViewById(R.id.edit_content); mAddButton = findViewById(R.id.btn_add); mDeleteButton = findViewById(R.id.btn_delete); mUpdateButton = findViewById(R.id.btn_update); mSearchButton = findViewById(R.id.btn_search); mPoemsListView = findViewById(R.id.list_poems); mDatabase = openOrCreateDatabase("poems.db", MODE_PRIVATE, null); String createTableSql = "CREATE TABLE IF NOT EXISTS poems (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT)"; mDatabase.execSQL(createTableSql); mAddButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String title = mTitleEditText.getText().toString(); String content = mContentEditText.getText().toString(); addPoem(title, content); mTitleEditText.setText(""); mContentEditText.setText(""); getAllPoems(); } }); mDeleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String title = mTitleEditText.getText().toString(); deletePoem(title); mTitleEditText.setText(""); getAllPoems(); } }); mUpdateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String title = mTitleEditText.getText().toString(); String content = mContentEditText.getText().toString(); updatePoem(title, content); mTitleEditText.setText(""); mContentEditText.setText(""); getAllPoems(); } }); mSearchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String title = mTitleEditText.getText().toString(); searchPoem(title); } }); getAllPoems(); } private void addPoem(String title, String content) { String insertSql = "INSERT INTO poems (title, content) VALUES (?, ?)"; SQLiteStatement statement = mDatabase.compileStatement(insertSql); statement.bindString(1, title); statement.bindString(2, content); statement.executeInsert(); } private void deletePoem(String title) { String deleteSql = "DELETE FROM poems WHERE title = ?"; SQLiteStatement statement = mDatabase.compileStatement(deleteSql); statement.bindString(1, title); statement.executeUpdateDelete(); } private void updatePoem(String title, String content) { String updateSql = "UPDATE poems SET content = ? WHERE title = ?"; SQLiteStatement statement = mDatabase.compileStatement(updateSql); statement.bindString(1, content); statement.bindString(2, title); statement.executeUpdateDelete(); } private void getAllPoems() { String selectSql = "SELECT * FROM poems"; Cursor cursor = mDatabase.rawQuery(selectSql, null); List<String> poems = new ArrayList<>(); while (cursor.moveToNext()) { String title = cursor.getString(cursor.getColumnIndex("title")); String content = cursor.getString(cursor.getColumnIndex("content")); String poem = title + ": " + content; poems.add(poem); } ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, poems); mPoemsListView.setAdapter(adapter); } private void searchPoem(String title) { String selectSql = "SELECT * FROM poems WHERE title = ?"; Cursor cursor = mDatabase.rawQuery(selectSql, new String[]{title}); List<String> poems = new ArrayList<>(); while (cursor.moveToNext()) { String content = cursor.getString(cursor.getColumnIndex("content")); String poem = title + ": " + content; poems.add(poem); } ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, poems); mPoemsListView.setAdapter(adapter); } } ``` 这个示例还可以进行更多的优化和完善,例如添加数据验证、异常处理等。但这应该足以让您理解如何在Android中使用SQLite数据库来创建一个简单的笔记本应用程序。

相关推荐

最新推荐

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

c++获取sqlite3数据库表中所有字段的方法小结

本文给大家分享c++获取sqlite3数据库表中所有字段的三种常用方法,本文针对每一种方法给大家详细介绍,需要的的朋友通过本文一起学习吧
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。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依