最新android实现一个简单的增删改查的数据库操作Word格式文档下载.docx
《最新android实现一个简单的增删改查的数据库操作Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《最新android实现一个简单的增删改查的数据库操作Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
//初始化
empname=(EditText)findViewById(R.id.empname);
empno=(EditText)findViewById(R.id.empno);
empage=(EditText)findViewById(R.id.empage);
empsal=(EditText)findViewById(R.id.empsal);
save=(Button)findViewById(R.id.save);
clear=(Button)findViewById(R.id.clear);
save.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Stringname=empname.getText().toString();
Stringno=empno.getText().toString();
Stringage=empage.getText().toString();
Stringsal=empsal.getText().toString();
if(name.length()>
0&
&
no.length()>
age.length()>
sal.length()>
0){
helper.insertEmp(name,no,age,sal);
Toast.makeText(AddEmpActivity.this,"
增加成功"
1000).show();
Intentintent=newIntent(AddEmpActivity.this,MainActivity.class);
startActivity(intent);
}
else{
增加失败"
}
});
clear.setOnClickListener(newOnClickListener(){
empname.setText("
"
);
empno.setText("
empage.setText("
empsal.setText("
}
}
主活动页面
importandroid.provider.ContactsContract.Data;
importandroid.provider.ContactsContract.CommonDataKinds.Phone;
importandroid.app.AlertDialog;
importandroid.app.AlertDialog.Builder;
importandroid.app.ListActivity;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.database.Cursor;
importandroid.view.ContextMenu;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.ContextMenu.ContextMenuInfo;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.AdapterContextMenuInfo;
importandroid.widget.CheckBox;
importandroid.widget.ListView;
importandroid.widget.RelativeLayout;
importandroid.widget.SimpleAdapter;
importandroid.widget.SimpleCursorAdapter;
importandroid.widget.TextView;
importandroid.widget.AdapterView.OnItemClickListener;
publicclassMainActivityextendsActivity{
privateButtoninsert,delete,update,select;
privateTextViewshow_name,show_no,show_age,show_sal;
privateListViewlv;
Cursorcursor;
publicint_id;
//privateBooleanbool=false;
privateMenumyMenu;
privateContextcontext;
SimpleCursorAdapteradapter;
setContentView(R.layout.activity_main);
//初始化控件
insert=(Button)findViewById(R.id.insert);
delete=(Button)findViewById(R.id.delete);
update=(Button)findViewById(R.id.update);
select=(Button)findViewById(R.id.select);
show_name=(TextView)findViewById(R.id.show_name);
show_no=(TextView)findViewById(R.id.show_no);
show_age=(TextView)findViewById(R.id.show_age);
show_sal=(TextView)findViewById(R.id.show_sal);
lv=(ListView)findViewById(R.id.lv);
registerForContextMenu(lv);
insert.setOnClickListener(listener);
delete.setOnClickListener(listener);
update.setOnClickListener(listener);
select.setOnClickListener(listener);
OnClickListenerlistener=newOnClickListener(){
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.insert:
Intentintent=newIntent(MainActivity.this,AddEmpActivity.class);
startActivity(intent);
break;
caseR.id.delete:
caseR.id.update:
caseR.id.select:
select();
}
};
//长按菜单
publicvoidonCreateContextMenu(ContextMenumenu,Viewv,
ContextMenuInfomenuInfo){
menu.setHeaderTitle("
快捷操作"
menu.add(Menu.NONE,1,1,"
删除"
menu.add(Menu.NONE,2,2,"
修改"
super.onCreateContextMenu(menu,v,menuInfo);
//显示数据在listview上
publicvoidselect(){
Cursorcursor=helper.getCursorScrollData();
adapter=newSimpleCursorAdapter(MainActivity.this,R.layout.list_item,cursor,newString[]{"
empname"
"
empno"
empage"
empsal"
},newint[]{R.id.show_name,R.id.show_no,R.id.show_age,R.id.show_sal});
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
//响应事件
publicbooleanonContextItemSelected(MenuItemitem){
//TODOAuto-generatedmethodstub
//这样可以获取长按着弹出上下文菜单的LIstView点击的ID值
AdapterView.AdapterContextMenuInfomenuInfo=(AdapterContextMenuInfo)item.getMenuInfo();
finalintid=(int)menuInfo.id;
if(-1==id){
super.onContextItemSelected(item);
switch(item.getItemId()){
case1:
Cursorcursor1=(Cursor)lv.getItemAtPosition(menuInfo.position);
cursor1.moveToPosition(menuInfo.position);
_id=cursor1.getInt(0);
Builderbuilder=newBuilder(MainActivity.this);
builder.setMessage("
你确定要删除?
AlertDialogd=builder.create();
d.setButton("
是"
newDialogInterface.OnClickListener(){
publicvoidonClick(DialogInterfacedialog,intwhich){
//TODOAuto-generatedmethodstub
helper.delete(_id);
select();
//更新
Toast.makeText(MainActivity.this,"
删除成功"
});
d.setButton2("
否"
newDialogInterface.OnClickListener(){
dialog.cancel();
d.show();
break;
case2:
Intentintent=newIntent(this,UpdateEmpActivity.class);
Cursorcursor=(Cursor)lv.getItemAtPosition(menuInfo.position);
cursor.moveToPosition(menuInfo.position);
_id=cursor.getInt(0);
Stringempname=String.valueOf(cursor.getString(cursor.getColumnIndex("
)));
Stringempno=String.valueOf(cursor.getString(cursor.getColumnIndex("
Stringempage=String.valueOf(cursor.getString(cursor.getColumnIndex("
Stringempsal=String.valueOf(cursor.getString(cursor.getColumnIndex("
//跳转页面
intent.putExtra("
empname);
empno);
empage);
empsal);
_id"
_id);
startActivity(intent);
returnsuper.onContextItemSelected(item);
publicclassUpdateEmpActivityextendsActivity{
privateDBHelperhelper=newDBHelper(this);
privateCursorcursor;
//获取传来的数据
Intentintent=this.getIntent();
_id=intent.getIntExtra("
0);
Stringemp_name=intent.getStringExtra("
Stringemp_no=intent.getStringExtra("
Stringemp_age=intent.getStringExtra("
Stringemp_sal=intent.getStringExtra("
//显示在文本框中
empname.setText(emp_name);
empno.setText(emp_no);
empage.setText(emp_age);
empsal.setText(emp_sal);
Stringa1=empname.getText().toString();
Stringa2=empno.getText().toString();
Stringa3=empage.getText().toString();
Stringa4=empsal.getText().toString();
helper.UpdateEmp(_id,a1,a2,a3,a4);
Toast.makeText(UpdateEmpActivity.this,"
修改成功"
5).show();
Intentintent=newIntent(UpdateEmpActivity.this,MainActivity.class);
酒店总经理年度个人工作汇报