irpas技术客

Android 记事本_睡不饱的我

irpas 7756

文本数据的存储添加删除

登陆注册

SQlite的创建

数据listview列表的显示

demo地址

My日记本App.zip-Android文档类资源-CSDN下载

?

package com.example.mydiaryapp;

import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull; import androidx.annotation.Nullable;

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

public class MySqlHelper extends SQLiteOpenHelper {

? ? String sql = "create table note(id integer primary key autoincrement ,title text,content text);"; ? ? SQLiteDatabase db;

? ? public MySqlHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { ? ? ? ? super(context, name, factory, version); ? ? ? ? db = getWritableDatabase(); ? ? }

? ? public MySqlHelper(Context context) { ? ? ? ? this(context, "noteDb", null, 1); ? ? }

? ? @Override ? ? public void onCreate(SQLiteDatabase db) { ? ? ? ? db.execSQL(sql); ? ? }

? ? public HashMap<String, String> getNote(String id) { ? ? ? ? HashMap map = new HashMap<String, String>(); ? ? ? ? Cursor cursor = db.query("note", null, "id=?", new String[]{id}, null, null, null); ? ? ? ? if (cursor.getCount() > 0) { ? ? ? ? ? ? cursor.moveToFirst(); ? ? ? ? ? ? int title_idx = cursor.getColumnIndex("title"); ? ? ? ? ? ? int content_idx = cursor.getColumnIndex("content"); ? ? ? ? ? ? String title = cursor.getString(title_idx); ? ? ? ? ? ? String content = cursor.getString(content_idx); ? ? ? ? ? ? map.put("title", title); ? ? ? ? ? ? map.put("content", content); ? ? ? ? } ? ? ? ? return map; ? ? }

? ? public void writeNote(String id, String title, String content) { ? ? ? ? ContentValues contentValues = new ContentValues(); ? ? ? ? contentValues.put("title", title); ? ? ? ? contentValues.put("content", content); ? ? ? ? if (id != null) { ? ? ? ? ? ? db.update("note", contentValues, "id=?", new String[]{id}); ? ? ? ? } else { ? ? ? ? ? ? long insert_id = db.insert("note", null, contentValues); ? ? ? ? }

? ? }

? ? public List<Map<String, String>> getNotes() { ? ? ? ? List<Map<String, String>> notes_data = new ArrayList<Map<String, String>>(); ? ? ? ? Cursor cursor = db.query("note", null, null, null, null, null, null);

? ? ? ? if (cursor.getCount() > 0) { ? ? ? ? ? ? cursor.moveToFirst(); ? ? ? ? ? ? int id_idx = cursor.getColumnIndex("id"); ? ? ? ? ? ? int title_idx = cursor.getColumnIndex("title"); ? ? ? ? ? ? int content_idx = cursor.getColumnIndex("content"); ? ? ? ? ? ? do { ? ? ? ? ? ? ? ? Map map = new HashMap<String, String>(); ? ? ? ? ? ? ? ? map.put("id", cursor.getString(id_idx)); ? ? ? ? ? ? ? ? map.put("title", cursor.getString(title_idx)); ? ? ? ? ? ? ? ? map.put("content", cursor.getString(content_idx)); ? ? ? ? ? ? ? ? notes_data.add(map);

? ? ? ? ? ? } while (cursor.moveToNext()); ? ? ? ? } ? ? ? ? return notes_data; ? ? } ? ? public void deleteNote(String id) { ? ? ? ? db.delete("note", "id=?", new String[]{id}); ? ? }

? ? public void flushDb() { ? ? ? ? db.delete("note", "1=1", null); ? ? }

? ? @Override ? ? public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

? ? } } ?


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Android #记事本