实验8 SQLite数据库操作
【目的】
设计一个个人通讯录,掌握Android平台下的数据库开发,该个人通讯录主要包括联系人列表和联系人详细信息等界面。
【要求】
程序主界面是通讯录的目录显示手机上联系人的名称。长按联系人的姓名可以显示联系人的详细信息。添加和删除按钮可以添加联系人和删除联系人。
【实验环境】
1)PC机
2)操作系统:Windows XP
3)软件: Eclipse, JDK1.6,Android SDK,ADT
【实验内容及步骤】
1)确定数据库的数据结构
2) 在res/drawable-mdpi目录下拷入程序要用的图标
3)定义字符串资源string.xml
4)开发布局文件activity_main.xml用于显示联系人列表
5)layout目录下新建一个detail.xml,用于显示联系人详细信息
6)开发数据库辅助类MyOpenHelper类,新建一个MyOpenHelper.java
7)接下来便进入MainActivity端的开发,实现数据库增加、删除、修改记录等操作
8)新建一个Activity名字叫DetailActivity.java,实现联系人详细信息显示功能
string.xml代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">contact</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="Name">姓名:</string> <string name="phone">固定电话:</string> <string name="mobile">移动电话:</string> <string name="email">电子邮件:</string> <string name="post">邮政编码:</string> <string name="addr">通信地址:</string> <string name="comp">公司名称:</string> <string name="add">添加</string> <string name="modify">修改</string> <string name="delete">删除</string> <string name="save">保存</string> <string name="message">确认删除此联系人吗?</string> <string name="ok">确定</string> <string name="cancel">取消</string> <string name="title">联系人列表</string> </resources>
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); } }
【实验结果截图】
【实验小结】
这次实验感觉很难,因为缺了几节课,什么都没学习到,但是请教了几位同学,勉强完成了一部分。