來源:CSDN_OF_ZHAO 發(fā)布時間:2018-11-30 17:46:26 閱讀量:1062
本Demo涉及知識點概述如下:
SQLite數(shù)據(jù)庫與SimpleCursorAdapter適配器
SQLite的常用語法
SQLiteOpenHelper的用法
SQLite數(shù)據(jù)庫實現(xiàn)CRUD的少參方法
ListView的數(shù)據(jù)展示
上下文菜單的展示
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="暫無聯(lián)系人!"
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="暫無聯(lián)系人!"
android:textColor="#00f"
android:textSize="16sp" />
</RelativeLayout>
XML(菜單布局):
<?xml version="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="添加聯(lián)系人"/>
</menu>
XML(menu文件夾下菜單布局):
<?xml version="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); // 設(shè)置空視圖
registerForContextMenu(lView); // 注冊上下文對象
initDataBase(); // 初始化數(shù)據(jù)庫
initData(); // ListView賦值
}
// 初始化數(shù)據(jù)庫
private void initDataBase() {
// TODO Auto-generated method stub
// 創(chuàng)建數(shù)據(jù)庫打開工具
OpenHelper helper = new OpenHelper(this);
// 獲取數(shù)據(jù)庫操作類 提成全局變量方便操作
db = helper.getReadableDatabase();
}
// ListView添加數(shù)據(jù)
private void initData() {
// cursor也提成全部變量 ,方便關(guān)閉
// cursor需要關(guān)閉,放置內(nèi)存溢出 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);
}
// 創(chuàng)建系統(tǒng)菜單 點擊菜單彈出 DiaLog 填充數(shù)據(jù)
@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
// 點擊添加聯(lián)系人
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.icon); // 設(shè)置圖片
builder.setTitle("添加聯(lián)系人");
// 把布局轉(zhuǎn)化成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); // 添加到數(shù)據(jù)庫
if (insert != -1) {
initData(); // 再次調(diào)用 當刷新使用
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(); // 取消直接關(guān)閉即可
}
});
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);
}
// 上下文菜單監(jiān)聽
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
final long id = info.id; // 單條對應(yīng)的數(shù)據(jù)庫_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("更新聯(lián)系人");
builder.setIcon(R.drawable.icon);
View view = LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog_view, null);
builder.setView(view); //設(shè)置自定義視圖
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);
//查數(shù)據(jù)顯示到EditText
//查數(shù)據(jù)
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(); //關(guān)閉
}
});
builder.show();
break;
}
return super.onContextItemSelected(item);
}
}
---------------------