时间:2021-07-01 10:21:17 帮助过:3人阅读
MainActivity.java部分代码:
public class MainActivity extends Activity { private DBOpenHelper dbOpenHelper; private ContantsAdapter contantsAdapter; private ListView dbListView; private Cursor cursor; final int MENU_ADD = Menu.FIRST; final int MENU_CHANGE = Menu.FIRST+1; private List<String> ids; private List<String> names; private List<String> phones; AlertDialog dialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbOpenHelper = new DBOpenHelper(this); dbListView = (ListView)findViewById(R.id.db_listview); refreshDBOpenHelper(); dbListView.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Intent intent= new Intent(); intent.putExtra("cmd", 0); String contants_id = ids.get(position);//0代表查询联系人,1代表添加联系人 intent.putExtra("id", contants_id); intent.setClass(MainActivity.this, DetailContantsActivity.class); startActivity(intent); } }); dbListView.setOnItemLongClickListener(new OnItemLongClickListener(){ public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3) { dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("提示!!") .setMessage("确定要删除这条记录?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String contants_id = ids.get(position); dbOpenHelper.deleteContants(contants_id); dbOpenHelper.close(); Toast.makeText(MainActivity.this, "正在删除数据库,请稍后。。。", Toast.LENGTH_LONG).show(); refreshDBOpenHelper(); contantsAdapter.notifyDataSetChanged(); } }) .setNegativeButton("取消",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }).show(); return false; } }); } @Override protected void onResume() { refreshDBOpenHelper(); contantsAdapter.notifyDataSetChanged(); super.onResume(); } public void refreshDBOpenHelper(){ cursor = dbOpenHelper.selectContants(); ids = new ArrayList<String>(); names = new ArrayList<String>(); phones = new ArrayList<String>(); int count = cursor.getCount(); if(count>0){ for(int i=0;i<count;i++){ cursor.moveToPosition(i); ids.add(cursor.getString(0)); names.add(cursor.getString(1)); phones.add(cursor.getString(2)); } }else{ Toast.makeText(this, R.string.not_dbcursor_values, Toast.LENGTH_SHORT).show(); } contantsAdapter = new ContantsAdapter(this,names,phones); dbListView.setAdapter(contantsAdapter); cursor.close(); dbOpenHelper.close(); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_ADD, 0, R.string.menu_add) .setIcon(R.drawable.add); //add - add_button menu.add(0, MENU_CHANGE, 0, R.string.menu_change) .setIcon(R.drawable.modify); //add -add_change_password return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ // case MENU_ADD: //press change add button Intent add_intent= new Intent(this,DetailContantsActivity.class); add_intent.putExtra("cmd", 1); startActivity(add_intent); break; case MENU_CHANGE: //press change password button Intent change_password_intent = new Intent(MainActivity.this,PasswordManage.class); startActivityForResult(change_password_intent,0); break; } return super.onOptionsItemSelected(item); } }
【实验结果截图】
【实验小结】
这次实验感觉很难,因为缺了几节课,什么都没学习到,但是请教了几位同学,勉强完成了一部分。
实验8 SQLite数据库操作
标签: