Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3(版本号)支持 NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型,SQLite可以解析大部分标准SQL语句
SQLiteOpenHelper对数据库进行管理,提供了两个重要的方法,分别是
onCreate(SQLiteDatabase db):初次使用软件时生成数据库表,SQLiteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法获取用于操作数据库的SQLiteDatabase实例的时候,若数据库不存在,Android系统会自动生成一个数据库,
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion):升级软件时更新数据库表结构
功能实现:
使用android内部的数据库SQLite添加日记数据库并进行日记的增、删、改、查操作
使用Menu菜单弹出菜单项进行添加、删除操作
主页面实现一个淡入效果
效果演示:
i
点击Menu菜单时弹出两个菜单项
代码实现:
1:创建数据库帮助类DBHelper:
下面是png格式的不能够复制代码,望大家见谅,但是亲们可以看看过程,自己编写记忆更深。
2:编写实体类Diary
public class Diary {
private int id;
private String title;//日记标题
private String content;//内容
private String pubdate;
public Diary(String title, String content) {
super();
this.title = title;
this.content = content;
}
public Diary(String title, String content, String pubdate) {
super();
this.title = title;
this.content = content;
this.pubdate = pubdate;
}
getId,setId //显示日记列表时,标题长度限制
public String getSubTitle() {
if (title.length() > 8)
return title.substring(0, 7) + "……";
return title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
getContent,setContent,getPubdate,setPubdate方法 }
}
3:编写对数据库的操作类
在进行操作时可以使用SQLite内的封装的增删改查方法
此是源码,但是有字节颜色大小设置,遮盖了源码,不方便查看,后面png格式会看得清楚些
public class DiaryDao {
private SQLiteDatabase sqLiteDatabase;
private DBHelper dbHelper;
// 构造方法,带有context参数,以便下一对象操作
public DiaryDao(Context context){
dbHelper=new DBHelper(context);
}
public void add(Diary diary) {
String sql = "insert into td_diary(title,content,pubdate) values(?,?,?)";
// 得到SQLite数据库
sqLiteDatabase = dbHelper.getWritableDatabase();
// 执行sql语句
sqLiteDatabase.execSQL(sql,new String[] { diary.getTitle(), diary.getContent(),
diary.getPubdate() });
}
public void delete(int id) {
String sql = "delete from td_diary where id=?";
// 得到SQLite数据库
sqLiteDatabase = dbHelper.getWritableDatabase();
// 执行sql语句
sqLiteDatabase.execSQL(sql, new String[] { Integer.toString(id) });
}
public void update(Diary diary) {
String sql = "update td_diary set title=?,content=? where id=?";
// 得到SQLite数据库
sqLiteDatabase = dbHelper.getWritableDatabase();
sqLiteDatabase.execSQL(
sql,
new String[] { diary.getTitle(), diary.getContent(),
Integer.toString(diary.getId()) });
}
public Diary select(int id) {
Diary diary =null;
String sql="select * from td_diary where id=?";
sqLiteDatabase=dbHelper.getReadableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery(sql, new String[]{Integer.toString(id)});
if(cursor.moveToFirst()){
String title=cursor.getString(cursor.getColumnIndex("title"));
String content=cursor.getString(cursor.getColumnIndex("content"));
String pubdate=cursor.getString(cursor.getColumnIndex("pubdate"));
diary = new Diary(title, content, pubdate);
}
return diary;
}
public List getAllDiarys(){
List list=new ArrayList();
sqLiteDatabase=dbHelper.getReadableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery("select * from td_diary", null);
while(cursor.moveToNext()){
int id=cursor.getInt(cursor.getColumnIndex("id"));
String title=cursor.getString(cursor.getColumnIndex("title"));
String content=cursor.getString(cursor.getColumnIndex("content"));
String pubdate=cursor.getString(cursor.getColumnIndex("pubdate"));
Diary diary = new Diary(title, content, pubdate);
diary.setId(id);
list.add(diary);
}
return list;
}
}
下面的就是截取的代码图片格式,不能够复制代码,若有用可复制上面的代码:
增、删、改、查方法
sqLiteDatabase.insert/delete/update/query
查找所有信息时可以返回Cursor游标类型,给其绑定SimpleCursorAdapter就可获取值
即:填充列表时用键,及数据库的列名。
4:主页面的淡入效果,然后几秒钟调转日记列表页面
public class MainActivity extends Activity {
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) this.findViewById(R.id.image);
image.setImageResource(R.drawable.tp);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);//alpha变化范围
animation.setDuration(3000);//显示时间
animation.setStartOffset(100);
image.setAnimation(animation);
// 新建一个handler实现演示跳转
new Handler().postDelayed(new Runnable() {
public void run() {
Intent i = new Intent(MainActivity.this, DiaryActivity.class);
startActivity(i);
}
}, 3000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
5:列表页面代码
public class DiaryActivity extends Activity {
private ListView listview;
private DiaryDao dao = new DiaryDao(this);
List
6:添加或更新日记布局代码
主页面布局:
7。更新日记java代码:
public class DiaryUpdateActivity extends Activity {
private EditText title;
private EditText content;
private Button save;
private DiaryDao dao;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_diary);
// 查找组件
title = (EditText) this.findViewById(R.id.edit1);
content = (EditText) this.findViewById(R.id.edit2);
save = (Button) this.findViewById(R.id.save);
// 获取上个视图传的参数值
Intent i = getIntent();
final int id = i.getIntExtra("id", 0);
System.out.println(id);
// 设置文本
title.setText(i.getStringExtra("title"));
content.setText(i.getStringExtra("content"));
// 添加事件点击处理监听
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Diary diary = new Diary( title.getText().toString(), content
.getText().toString());
diary.setId(id);
//执行更新
dao=new DiaryDao(DiaryUpdateActivity.this);
dao.update(diary);
//提示
Toast.makeText(DiaryUpdateActivity.this, "保存成功",
Toast.LENGTH_LONG).show();
//更新后跳转到日志列表视图
Intent in = new Intent(DiaryUpdateActivity.this,
DiaryActivity.class);
startActivity(in);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.update_diary, menu);
return true;
}
}
菜单布局代码:
列表项的布局代码:
任何程序都要理清思绪之后再编码,学习会更加有效率。