个人日记本(SQLite实现增删改查)

个人日记本(SQLite实现增删改查),第1张

概述效果图: 当程序运行后,首先进入日记列表,显示如图: 长时间点击某个日记,将跳转至修改日记界面: 点击menu后,显示如图: 点击编辑新日志将跳转至添加新日记界面: 选中某个日记,点击删除日记,将d出提示框: 点击“确定”,删除日记,点击“取消”,返回日记列表页面 具体实现方法: 实体类: package cn.bzu.myd
效果图:

当程序运行后,首先进入日记列表,显示如图:

长时间点击某个日记,将跳转至修改日记界面:

点击menu后,显示如图:

点击编辑新日志将跳转至添加新日记界面:

选中某个日记,点击删除日记,将d出提示框:

点击“确定”,删除日记,点击“取消”,返回日记列表页面

具体实现方法:

实体类:

package cn.bzu.mydiary.model;public class Diary {	private int ID;	private String Title;	private String content;	private String pubdate;	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 getPubdate() {		return pubdate;	}	public voID setPubdate(String pubdate) {		this.pubdate = pubdate;	}			public Diary(String Title,String content,String pubdate) {		super();		this.Title = Title;		this.content = content;		this.pubdate = pubdate;	}	public Diary(int ID,String Title,String pubdate) {		super();		this.ID = ID;		this.Title = Title;		this.content = content;		this.pubdate = pubdate;	}	public Diary(int ID,String content) {		super();		this.ID = ID;		this.Title = Title;		this.content = content;			}	}

创建数据库的类(数据库保存在DDMS/data/data/cn.bzu.mydiary/database/diary.db)

package cn.bzu.mydiary.db;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;public class DBHelper extends sqliteOpenHelper{	private static String DATABASE_name="diary.db";	private static int DATABASE_VERSION=1;	public DBHelper(Context context) {		super(context,DATABASE_name,null,DATABASE_VERSION);			}	@OverrIDe	public voID onCreate(sqliteDatabase db) {		db.execsql("create table tb_diary(_ID integer primary key autoincrement,Title verchar(20),content verchar(1000),pubdate)");			}	@OverrIDe	public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {					}}

数据库 *** 作类

package cn.bzu.mydiary.service;import java.util.ArrayList;import java.util.List;import java.util.Map;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import cn.bzu.mydiary.db.DBHelper;import cn.bzu.mydiary.model.Diary;public class DiaryService {	private DBHelper dbHelper;	sqliteDatabase sqliteDatabase = null;	public DiaryService(Context context) {		dbHelper = new DBHelper(context);	}	/**	 * 保存日记	 * 	 * @param diary	 */	public voID save(Diary diary) {		sqliteDatabase = dbHelper.getWritableDatabase();		String sql = "insert into tb_diary(Title,content,pubdate) values (?,?,?)";		sqliteDatabase.execsql(				sql,new String[] { diary.getTitle(),diary.getContent(),diary.getPubdate() });		sqliteDatabase.close();	}	// 查询日志	public List<Diary> getAllDiary() {		Diary diary = null;		List<Diary> diaryList = new ArrayList<Diary>();		sqliteDatabase = dbHelper.getReadableDatabase();		// 得到游标,最多只有一条数据		Cursor cursor = sqliteDatabase.rawquery("select * from tb_diary",null);		while (cursor.movetoNext()) {			int ID = cursor.getInt(0);			String Title = cursor.getString(cursor.getColumnIndex("Title"));			String content = cursor.getString(cursor.getColumnIndex("content"));			String pubdate = cursor.getString(cursor.getColumnIndex("pubdate"));			diary = new Diary(ID,Title,pubdate);			diaryList.add(diary);		}		cursor.close();		sqliteDatabase.close();		return diaryList;	}	/**	 * 根据ID删除日记	 * 	 * @param ID	 *            日记的ID号	 */	public voID delete(Integer ID) {		sqliteDatabase = dbHelper.getReadableDatabase();		sqliteDatabase.execsql("delete from tb_diary where _ID=?",new Object[] { ID });		sqliteDatabase.close();	}	/**	 * 更新日记	 * 	 * @param diary	 */	public voID update(Diary diary) {		sqliteDatabase = dbHelper.getReadableDatabase();		sqliteDatabase.execsql(				"update tb_diary set Title=?,content=?,pubdate=? where _ID=?",new Object[] { diary.getTitle(),diary.getPubdate(),diary.getID() });		sqliteDatabase.close();	}	// //获取记录总数	// public long count(){	// long count=0;	// sqliteDatabase sqliteDatabase=dbHelper.getReadableDatabase();	// Cursor cursor=sqliteDatabase.rawquery("select count(*) from tb_diary",// null);	// cursor.movetoFirst();	// count=cursor.getLong(0);	// return count;	// }}

相应的布局文件:activity_add_diary.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:background="@drawable/back"    androID:orIEntation="vertical" >    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="@string/Title" />    <EditText        androID:ID="@+ID/Title"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" />    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="@string/content" />    <EditText        androID:ID="@+ID/content"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:inputType="textMultiline"        androID:lines="6" />    <button        androID:ID="@+ID/save"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="@string/save" /></linearLayout>

activity_diary.xml

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="fill_parent"     androID:background="@drawable/back"    androID:layout_height="fill_parent" >    <ListVIEw        androID:ID="@+ID/diary"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" >    </ListVIEw>    <relativeLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" >        <button            androID:ID="@+ID/add"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/add"            androID:visibility="invisible" />        <button            androID:ID="@+ID/delete"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/delete"            androID:visibility="invisible" />    </relativeLayout></relativeLayout>

List_item.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"    androID:orIEntation="horizontal" >    <TextVIEw        androID:ID="@+ID/Title"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:gravity="left" />    <TextVIEw        androID:ID="@+ID/pubdate"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:gravity="right" /></linearLayout>

DiaryActivity.java

package cn.bzu.mydiary;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import cn.bzu.mydiary.model.Diary;import cn.bzu.mydiary.service.DiaryService;import androID.os.Bundle;import androID.app.Activity;import androID.app.AlertDialog;import androID.content.DialogInterface;import androID.content.Intent;import androID.database.Cursor;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.AdapterVIEw.OnItemLongClickListener;import androID.Widget.AdapterVIEw.OnItemSelectedListener;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;public class DiaryActivity extends Activity {	private List<Map<String,?>> diary;	private ListVIEw diaryList;	private button add,delete;	DiaryService diaryService;	AlertDialog deleteDiaryAlert;	SimpleAdapter simpleAdapter;	int ID;	@OverrIDe	public voID onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentVIEw(R.layout.activity_diary);		onRestart();	}	@OverrIDe	protected voID onRestart() {		super.onRestart();		diaryList = (ListVIEw) this.findVIEwByID(R.ID.diary);		add = (button) this.findVIEwByID(R.ID.add);		delete = (button) this.findVIEwByID(R.ID.delete);		diary = getDiary();		simpleAdapter = new SimpleAdapter(this,diary,R.layout.List_item,new String[] { "Title","pubdate" },new int[] { R.ID.Title,R.ID.pubdate });		diaryList.setAdapter(simpleAdapter);		//获取删除时需要的ID		diaryList.setonItemClickListener(new OnItemClickListener() {			public voID onItemClick(AdapterVIEw<?> arg0,VIEw arg1,int position,long arg3) {				ID = (Integer) diary.get(position).get("ID");							}		});		//长时间按该日志,可以跳转至修改页面		diaryList.setonItemLongClickListener(new OnItemLongClickListener() {			public boolean onItemLongClick(AdapterVIEw<?> arg0,long arg3) {				ID = (Integer) diary.get(position).get("ID");				String Title = (String) diary.get(position).get("Title");				String content = (String) diary.get(position).get("content");				Diary d = new Diary(ID,content);				Intent intent = new Intent();				intent.setClass(DiaryActivity.this,AddDiaryActivity.class);				Bundle bundle = new Bundle();				bundle.putInt("ID",ID);				bundle.putString("Title",Title);				bundle.putString("content",content);				intent.putExtra("diary",bundle);				startActivity(intent);				return false;			}		});	}	private List<Map<String,?>> getDiary() {		List<Map<String,?>> data = new ArrayList<Map<String,?>>();		Map<String,Object> item = null;		diaryService = new DiaryService(DiaryActivity.this);		List<Diary> List = diaryService.getAllDiary();		for (int i = 0; i < List.size(); i++) {			Diary d = List.get(i);			item = new HashMap<String,Object>();			item.put("ID",d.getID());// ID			item.put("Title",d.getTitle());// 标题			item.put("content",d.getContent());			item.put("pubdate",d.getPubdate());// 出版日期			data.add(item);		}		return data;	}	@OverrIDe	public boolean onCreateOptionsMenu(Menu menu) {		super.onCreateOptionsMenu(menu);		// 创建菜单,并设置图表		menu.add(0,R.ID.add,"编辑新日志").setIcon(				androID.R.drawable.ic_input_add);		menu.add(0,R.ID.delete,1,"删除该日志").setIcon(				androID.R.drawable.ic_input_delete);		return true;	}	@OverrIDe	public boolean onoptionsItemSelected(MenuItem item) {		switch (item.getItemID()) {		case R.ID.add:			Intent intent = new Intent();			intent.setClass(DiaryActivity.this,AddDiaryActivity.class);			startActivity(intent);			break;		case R.ID.delete:			deleteDiaryDialog();			deleteDiaryAlert.show();			break;		}		return super.onoptionsItemSelected(item);	}	// 删除日志	private voID deleteDiaryDialog() {		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);		alertDialog.setTitle("请确定是否删除日记");		alertDialog.setPositivebutton("确定",new DialogInterface.OnClickListener() {					public voID onClick(DialogInterface dialog,int which) {						 						// System.out.print("ID:"+ID);						diaryService.delete(ID);						onRestart();					}				});		alertDialog.setNegativebutton("取消",int which) {						dialog.cancel();					}				});		deleteDiaryAlert = alertDialog.create();	}}

AddDiaryActivity.java

package cn.bzu.mydiary;import java.text.SimpleDateFormat;import java.util.Date;import cn.bzu.mydiary.model.Diary;import cn.bzu.mydiary.service.DiaryService;import androID.os.Bundle;import androID.app.Activity;import androID.content.Intent;import androID.graphics.Shader.TileMode;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.support.v4.app.NavUtils;public class AddDiaryActivity extends Activity {	private EditText TitleText;	private EditText contentText;	private button save;	int ID;	    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_add_diary);        TitleText=(EditText) this.findVIEwByID(R.ID.Title);        contentText=(EditText) this.findVIEwByID(R.ID.content);        save=(button) this.findVIEwByID(R.ID.save);        final Bundle bundle = this.getIntent().getBundleExtra("diary");        if(bundle==null){        	 save.setonClickListener(new OnClickListener() {     			     			public voID onClick(VIEw v) {     				String Title=TitleText.getText().toString();     				String content=contentText.getText().toString();     				DiaryService diaryService=new DiaryService(AddDiaryActivity.this);     				Diary diary=new Diary(Title,fomate());     				diaryService.save(diary);     				Intent intent=new Intent();     				intent.setClass(AddDiaryActivity.this,DiaryActivity.class);     				startActivity(intent);     			}     		});        }else{        	 TitleText.setText(bundle.getString("Title"));             contentText.setText(bundle.getString("content"));             ID =bundle.getInt("ID");             save.setonClickListener(new OnClickListener() {      			      			public voID onClick(VIEw v) {      				String Title=TitleText.getText().toString();      				String content=contentText.getText().toString();      				DiaryService diaryService=new DiaryService(AddDiaryActivity.this);      				Diary diary=new Diary(ID,fomate());      				diaryService.update(diary);      				Intent intent=new Intent();      				intent.setClass(AddDiaryActivity.this,DiaryActivity.class);      				startActivity(intent);      			}      		});        }                   }    public String fomate(){		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日  hh时:mm分:ss秒");		return simpleDateFormat.format(new Date());	}    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_add_diary,menu);        return true;    }    }


可以先建测试类测试一下,确认无误后,再运行,若要测试勿忘在AndroIDManifest.xml中配置哦


package cn.bzu.mydiary;import java.text.SimpleDateFormat;import java.util.Date;import cn.bzu.mydiary.model.Diary;import cn.bzu.mydiary.service.DiaryService;import androID.os.Bundle;import androID.app.Activity;import androID.content.Intent;import androID.graphics.Shader.TileMode;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.support.v4.app.NavUtils;public class AddDiaryActivity extends Activity {	private EditText TitleText;	private EditText contentText;	private button save;	int ID;	    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_add_diary);        TitleText=(EditText) this.findVIEwByID(R.ID.Title);        contentText=(EditText) this.findVIEwByID(R.ID.content);        save=(button) this.findVIEwByID(R.ID.save);        final Bundle bundle = this.getIntent().getBundleExtra("diary");        if(bundle==null){        	 save.setonClickListener(new OnClickListener() {     			     			public voID onClick(VIEw v) {     				String Title=TitleText.getText().toString();     				String content=contentText.getText().toString();     				DiaryService diaryService=new DiaryService(AddDiaryActivity.this);     				Diary diary=new Diary(Title,menu);        return true;    }    }



package cn.bzu.mydiary;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import cn.bzu.mydiary.model.Diary;import cn.bzu.mydiary.service.DiaryService;import androID.os.Bundle;import androID.app.Activity;import androID.app.AlertDialog;import androID.content.DialogInterface;import androID.content.Intent;import androID.database.Cursor;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.AdapterVIEw.OnItemLongClickListener;import androID.Widget.AdapterVIEw.OnItemSelectedListener;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;public class DiaryActivity extends Activity {	private List<Map<String,int which) {						dialog.cancel();					}				});		deleteDiaryAlert = alertDialog.create();	}}



<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"    androID:orIEntation="horizontal" >    <TextVIEw        androID:ID="@+ID/Title"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:gravity="left" />    <TextVIEw        androID:ID="@+ID/pubdate"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:gravity="right" /></linearLayout>



<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="fill_parent"     androID:background="@drawable/back"    androID:layout_height="fill_parent" >    <ListVIEw        androID:ID="@+ID/diary"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" >    </ListVIEw>    <relativeLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" >        <button            androID:ID="@+ID/add"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/add"            androID:visibility="invisible" />        <button            androID:ID="@+ID/delete"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/delete"            androID:visibility="invisible" />    </relativeLayout></relativeLayout>



<?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:background="@drawable/back"    androID:orIEntation="vertical" >    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="@string/Title" />    <EditText        androID:ID="@+ID/Title"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" />    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="@string/content" />    <EditText        androID:ID="@+ID/content"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:inputType="textMultiline"        androID:lines="6" />    <button        androID:ID="@+ID/save"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="@string/save" /></linearLayout>



package cn.bzu.mydiary.service;import java.util.ArrayList;import java.util.List;import java.util.Map;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import cn.bzu.mydiary.db.DBHelper;import cn.bzu.mydiary.model.Diary;public class DiaryService {	private DBHelper dbHelper;	sqliteDatabase sqliteDatabase = null;	public DiaryService(Context context) {		dbHelper = new DBHelper(context);	}	/**	 * 保存日记	 * 	 * @param diary	 */	public voID save(Diary diary) {		sqliteDatabase = dbHelper.getWritableDatabase();		String sql = "insert into tb_diary(Title,// null);	// cursor.movetoFirst();	// count=cursor.getLong(0);	// return count;	// }}



package cn.bzu.mydiary.db;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;public class DBHelper extends sqliteOpenHelper{	private static String DATABASE_name="diary.db";	private static int DATABASE_VERSION=1;	public DBHelper(Context context) {		super(context,int newVersion) {					}}
总结

以上是内存溢出为你收集整理的个人日记本(SQLite实现增删改查)全部内容,希望文章能够帮你解决个人日记本(SQLite实现增删改查)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1174888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存