Android开发_记事本(6)
删除键的功能实现
删除当前笔记文件
EditActivity中
添加
@Override
public boolean onOptionsItemSelected(MenuItem item){ //监听状态栏上内容被点击
switch (item.getItemId()){
case R.id.delete:
new AlertDialog.Builder(EditActivity.this)//创建一个点击按钮后出现的对话框选项(是or否删除)
.setMessage("删除吗?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {//点击是按钮后的反应
@Override
public void onClick(DialogInterface dialog, int which) {
if (openMode == 4){ // new note
intent.putExtra("mode", -1);
setResult(RESULT_OK, intent);
}
else { // existing note
intent.putExtra("mode", 2);
intent.putExtra("id", id); //笔记id
setResult(RESULT_OK, intent);
}
finish();
} //这部分的数据会被传回MianActivity,OnActivityForResult接受
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {//点击取消,关闭弹出的对话框
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show(); //builder相当于模型,create和show创建、显示出来上面的模型
break;
}
return super.onOptionsItemSelected(item);
}
如果点击删除按钮,上面的代码传回MainActivity一个“2”值
下面在MainActivity中接收
MainActivity
新增returnMode=2的部分
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int returnMode;
long note_Id;
returnMode = data.getExtras().getInt("mode", -1);
note_Id = data.getExtras().getLong("id", 0);
if (returnMode == 1) { //update current note
String content = data.getExtras().getString("content");
String time = data.getExtras().getString("time");
int tag = data.getExtras().getInt("tag", 1);
Note newNote = new Note(content, time, tag);
newNote.setId(note_Id);
CRUD op = new CRUD(context);
op.open();
op.updateNote(newNote);
op.close();
} else if (returnMode == 0) { // create new note
String content = data.getExtras().getString("content");
String time = data.getExtras().getString("time");
int tag = data.getExtras().getInt("tag", 1);
Note newNote = new Note(content, time, tag);
CRUD op = new CRUD(context);
op.open();
op.addNote(newNote);
op.close();
} else if (returnMode == 2) { // delete
Note curNote = new Note(); //创建一个“替身”
curNote.setId(note_Id);
CRUD op = new CRUD(context);
op.open();
op.removeNote(curNote);
op.close();
}
else{
}
refreshListView();
super.onActivityResult(requestCode, resultCode, data);
}
效果
删除全部的按钮功能实现
MainActivity
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_clear: //按钮的id
new AlertDialog.Builder(MainActivity.this)
.setMessage("删除全部吗?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dbHelper = new NoteDatabase(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("notes", null, null);//参数:删除的表名、限定条件、null
db.execSQL("update sqlite_sequence set seq=0 where name='notes'");//执行sql语句,功能:让id值归零,而不是从上一个已经删除的笔记id开始
refreshListView();
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
break;
}
return super.onOptionsItemSelected(item);
}
和上面删除单条笔记内容类似
但onClick内不同,进行了一些数据库操作
public void onClick(DialogInterface dialog, int which) {
dbHelper = new NoteDatabase(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("notes", null, null);//参数:删除的表名、限定条件、null
db.execSQL("update sqlite_sequence set seq=0 where name='notes'");//执行sql语句,功能:让id值归零,而不是从上一个已经删除的笔记id开始
refreshListView();
}
功能到此完成