您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > Java > android实现一个简单的增删改查的数据库操作
效果图‘packagecom.example.empmanager;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;publicclassAddEmpActivityextendsActivity{privateEditTextempname,empno,empage,empsal;privateButtonsave,clear;DBHelperhelper=newDBHelper(this);MainActivityactivity=newMainActivity();empInfoemp;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.add_emp);//初始化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-generatedmethodstubStringname=empname.getText().toString();Stringno=empno.getText().toString();Stringage=empage.getText().toString();Stringsal=empsal.getText().toString();if(name.length()0&&no.length()0&&age.length()0&&sal.length()0){helper.insertEmp(name,no,age,sal);Toast.makeText(AddEmpActivity.this,增加成功,1000).show();Intentintent=newIntent(AddEmpActivity.this,MainActivity.class);startActivity(intent);}else{Toast.makeText(AddEmpActivity.this,增加失败,1000).show();}}});clear.setOnClickListener(newOnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubempname.setText();empno.setText();empage.setText();empsal.setText();}});}}主活动页面packagecom.example.empmanager;importandroid.os.Bundle;importandroid.provider.ContactsContract.Data;importandroid.provider.ContactsContract.CommonDataKinds.Phone;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.app.AlertDialog.Builder;importandroid.app.ListActivity;importandroid.content.Context;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.view.ContextMenu;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.ContextMenu.ContextMenuInfo;importandroid.view.View.OnClickListener;importandroid.widget.AdapterView;importandroid.widget.AdapterView.AdapterContextMenuInfo;importandroid.widget.Button;importandroid.widget.CheckBox;importandroid.widget.ListView;importandroid.widget.RelativeLayout;importandroid.widget.SimpleAdapter;importandroid.widget.SimpleCursorAdapter;importandroid.widget.TextView;importandroid.widget.Toast;importandroid.widget.AdapterView.OnItemClickListener;publicclassMainActivityextendsActivity{privateButtoninsert,delete,update,select;privateTextViewshow_name,show_no,show_age,show_sal;privateListViewlv;DBHelperhelper=newDBHelper(this);empInfoemp;Cursorcursor;publicint_id;//privateBooleanbool=false;privateMenumyMenu;privateContextcontext;SimpleCursorAdapteradapter;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);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:break;caseR.id.update:break;caseR.id.select:select();break;}}};//长按菜单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(D
本文标题:android实现一个简单的增删改查的数据库操作
链接地址:https://www.777doc.com/doc-5001065 .html