Android studio设计备忘录,要求实现增删改查功能的代码

时间: 2024-01-22 16:20:12 浏览: 28
以下是一个简单的备忘录实现增删改查功能的代码,使用的是SQLite数据库: ``` public class MemoDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "memo.db"; private static final int DATABASE_VERSION = 1; public MemoDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_MEMO_TABLE = "CREATE TABLE " + MemoContract.MemoEntry.TABLE_NAME + "(" + MemoContract.MemoEntry._ID + " INTEGER PRIMARY KEY," + MemoContract.MemoEntry.COLUMN_TITLE + " TEXT," + MemoContract.MemoEntry.COLUMN_CONTENT + " TEXT," + MemoContract.MemoEntry.COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP" + ")"; db.execSQL(CREATE_MEMO_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + MemoContract.MemoEntry.TABLE_NAME); onCreate(db); } public long insertMemo(String title, String content) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(MemoContract.MemoEntry.COLUMN_TITLE, title); values.put(MemoContract.MemoEntry.COLUMN_CONTENT, content); long id = db.insert(MemoContract.MemoEntry.TABLE_NAME, null, values); db.close(); return id; } public List<Memo> getAllMemos() { List<Memo> memos = new ArrayList<>(); String selectQuery = "SELECT * FROM " + MemoContract.MemoEntry.TABLE_NAME + " ORDER BY " + MemoContract.MemoEntry.COLUMN_TIMESTAMP + " DESC"; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { Memo memo = new Memo(); memo.setId(cursor.getInt(cursor.getColumnIndex(MemoContract.MemoEntry._ID))); memo.setTitle(cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TITLE))); memo.setContent(cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_CONTENT))); memo.setTimestamp(cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TIMESTAMP))); memos.add(memo); } while (cursor.moveToNext()); } db.close(); return memos; } public Memo getMemoById(long id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(MemoContract.MemoEntry.TABLE_NAME, new String[]{MemoContract.MemoEntry._ID, MemoContract.MemoEntry.COLUMN_TITLE, MemoContract.MemoEntry.COLUMN_CONTENT, MemoContract.MemoEntry.COLUMN_TIMESTAMP}, MemoContract.MemoEntry._ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Memo memo = new Memo( cursor.getInt(cursor.getColumnIndex(MemoContract.MemoEntry._ID)), cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TITLE)), cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_CONTENT)), cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TIMESTAMP)) ); db.close(); return memo; } public int updateMemo(Memo memo) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(MemoContract.MemoEntry.COLUMN_TITLE, memo.getTitle()); values.put(MemoContract.MemoEntry.COLUMN_CONTENT, memo.getContent()); return db.update(MemoContract.MemoEntry.TABLE_NAME, values, MemoContract.MemoEntry._ID + " = ?", new String[]{String.valueOf(memo.getId())}); } public void deleteMemo(long id) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(MemoContract.MemoEntry.TABLE_NAME, MemoContract.MemoEntry._ID + " = ?", new String[]{String.valueOf(id)}); db.close(); } } ``` Memo.java: ``` public class Memo { private int id; private String title; private String content; private String timestamp; public Memo() { } public Memo(int id, String title, String content, String timestamp) { this.id = id; this.title = title; this.content = content; this.timestamp = timestamp; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } } ``` MemoContract.java: ``` public final class MemoContract { private MemoContract() {} public static class MemoEntry implements BaseColumns { public static final String TABLE_NAME = "memo"; public static final String COLUMN_TITLE = "title"; public static final String COLUMN_CONTENT = "content"; public static final String COLUMN_TIMESTAMP = "timestamp"; } } ``` 然后在你的Activity或Fragment中,你可以如下使用: ``` public class MemoActivity extends AppCompatActivity { private MemoDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_memo); dbHelper = new MemoDatabaseHelper(this); // 插入备忘录 long memoId = dbHelper.insertMemo("Title", "Content"); // 获取所有备忘录 List<Memo> memos = dbHelper.getAllMemos(); // 获取单个备忘录 Memo memo = dbHelper.getMemoById(memoId); // 更新备忘录 memo.setTitle("New Title"); dbHelper.updateMemo(memo); // 删除备忘录 dbHelper.deleteMemo(memoId); } } ``` 当然,你还需要在你的布局文件中添加相应的UI元素,例如EditText、ListView等等,以显示备忘录的内容。

相关推荐

最新推荐

recommend-type

Android连接MySQL数据库并进行增删改查操作示例讲解

主要介绍了Android 连接MySQL数据库并进行增删改查操作示例讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

android之sqlite实现增删改查

这是学习及开发SQlite的一个很好的例子,含有其增删改查的所有功能 附有完整的代码 可以直接运行
recommend-type

android studio实现简单考试应用程序实例代码详解

主要介绍了android studio实现简单考试应用程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android Studio 实现九宫格功能

主要介绍了Android Studio 实现九宫格,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

解决Android Studio 代码自动提示突然失效的问题

主要介绍了解决Android Studio 代码自动提示突然失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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