本Demo涉及知识点概述如下:
SQLite数据库与SimpleCursorAdapter适配器
SQLite的常用语法
SQLiteOpenHelper的用法
SQLite数据库实现CRUD的少参方法
ListView的数据展示
上下文菜单的展示
XML(主界面布局):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg_color" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > ListView> <TextView android:id="@+id/tv_empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="暂无联系人!" android:textColor="#00f" android:textSize="16sp" /> RelativeLayout>XML(ListView单条目布局):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg_color" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > ListView> <TextView android:id="@+id/tv_empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="暂无联系人!" android:textColor="#00f" android:textSize="16sp" /> RelativeLayout>XML(菜单布局):
"1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#AAAAAA" android:orientation="vertical" > <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:hint="请输入名称" > <requestFocus /> EditText> <EditText android:id="@+id/et_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:hint="请输入性别" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:hint="请输入手机号" android:inputType="phone" /> LinearLayout>XML(menu文件夹下菜单布局):
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_insert" android:icon="@drawable/add" android:orderInCategory="100" android:showAsAction="always" android:title="添加联系人"/> menu>XML(menu文件夹下菜单布局):
"1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_delete" android:title="删除"/> <item android:id="@+id/action_update" android:title="更新"/> menu>Java代码示例:
public class MainActivity extends Activity { private ListView lView; private TextView empty; private SQLiteDatabase db; private Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lView = (ListView) findViewById(R.id.lv); empty = (TextView) findViewById(R.id.tv_empty); lView.setEmptyView(empty); // 设置空视图 registerForContextMenu(lView); // 注册上下文对象 initDataBase(); // 初始化数据库 initData(); // ListView赋值 } // 初始化数据库 private void initDataBase() { // TODO Auto-generated method stub // 创建数据库打开工具 OpenHelper helper = new OpenHelper(this); // 获取数据库操作类 提成全局变量方便操作 db = helper.getReadableDatabase(); } // ListView添加数据 private void initData() { // cursor也提成全部变量 ,方便关闭 // cursor需要关闭,放置内存溢出 oom cursor = db.rawQuery("select * from contact", null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, new String[] { "_id", "name", "sex", "number" }, new int[] { R.id.tv_id, R.id.tv_name, R.id.tv_sex, R.id.tv_phoneNumber }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lView.setAdapter(adapter); } // 创建系统菜单 点击菜单弹出 DiaLog 填充数据 @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub // 点击添加联系人 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.icon); // 设置图片 builder.setTitle("添加联系人"); // 把布局转化成View View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null); // 自定义Dialog builder.setView(view); final EditText namEditText = (EditText) view.findViewById(R.id.et_name); final EditText sexEditText = (EditText) view.findViewById(R.id.et_sex); final EditText phoneEditText = (EditText) view .findViewById(R.id.et_phone); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // 添加非空判断 // 添加操作 ContentValues cValues = new ContentValues(); cValues.put("name", namEditText.getText() + ""); cValues.put("sex", sexEditText.getText() + ""); cValues.put("number", phoneEditText.getText() + ""); long insert = db.insert("contact", null, cValues); // 添加到数据库 if (insert != -1) { initData(); // 再次调用 当刷新使用 Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); // 取消直接关闭即可 } }); builder.show(); // show方法 千万别忘了 return super.onOptionsItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.contextmenu_listview_main, menu); AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; // info.id menu.setHeaderTitle("id:" + info.id); } // 上下文菜单监听 @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); final long id = info.id; // 单条对应的数据库_id; switch (item.getItemId()) { case R.id.action_delete: db.execSQL("delete from contact where _id =?", new Object[] { id }); initData(); break; case R.id.action_update: // 删除和更新 AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("更新联系人"); builder.setIcon(R.drawable.icon); View view = LayoutInflater.from(MainActivity.this).inflate( R.layout.dialog_view, null); builder.setView(view); //设置自定义视图 final EditText namEditText = (EditText) view.findViewById(R.id.et_name); final EditText sexEditText = (EditText) view.findViewById(R.id.et_sex); final EditText phoneEditText = (EditText) view .findViewById(R.id.et_phone); //查数据显示到EditText //查数据 Cursor cursor = db.rawQuery("select * from contact where _id = ?", new String[]{""+id}); cursor.moveToFirst(); //游标移动到第一行 namEditText.setText(cursor.getString(cursor.getColumnIndex("name"))); sexEditText.setText(cursor.getString(cursor.getColumnIndex("sex"))); phoneEditText.setText(cursor.getString(cursor.getColumnIndex("number"))); builder.setPositiveButton("修改", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub ContentValues contentValues = new ContentValues(); contentValues.put("name", namEditText.getText()+""); contentValues.put("sex", sexEditText.getText()+""); contentValues.put("number", phoneEditText.getText()+""); db.update("contact", contentValues, "_id=?", new String []{id+""}); initData(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); //关闭 } }); builder.show(); break; } return super.onContextItemSelected(item); } }