標籤:
實驗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); }}
【實驗結果】
【實驗小結】
這次實驗感覺很難,因為缺了幾節課,什麼都沒學習到,但是請教了幾位同學,勉強完成了一部分。
實驗8 SQLite資料庫操作