資料庫32範例:小戴修正。
package ex32.MyHotline;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener
{
static final String DB_NAME = "HotlineDB";
static final String TB_NAME = "hotlist";
static final String[] FROM = new String[]{"name", "phone", "email","id"};
SQLiteDatabase db;
Cursor cur;
SimpleCursorAdapter adapter;
EditText etName, etPhone, etEmail,etid;
Button btInsert, btUpdate, btDelete;
ListView lv;
ContentValues cv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etPhone = (EditText) findViewById(R.id.etPhone);
etEmail = (EditText) findViewById(R.id.etEmail);
etid = (EditText) findViewById(R.id.etid);
btInsert = (Button) findViewById(R.id.btInsert);
btUpdate = (Button) findViewById(R.id.btUpdate);
btDelete = (Button) findViewById(R.id.btDelete);
lv = (ListView) findViewById(R.id.lv);
lv.setOnItemClickListener(this); // 設定按下事件的監聽器
// 開啟或建立資料庫
db = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
// 建立資料表
String createTable = "CREATE TABLE IF NOT EXISTS " + TB_NAME +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + // 索引欄位
"name VARCHAR(32), " +
"phone VARCHAR(16), " +
"email VARCHAR(64),"+
"id VARCHAR(20))";
db.execSQL(createTable);
cur = db.rawQuery("SELECT * FROM " + TB_NAME, null);
// 若查詢結果是空的則寫入 2 筆測試資料
if (cur.getCount() == 0) //取得資料表列數
{
add_data("Yahoo", "02-12345678", "service@yahoo.com.tw","1234");
add_data("PChome", "02-87654321", "service@pchome.com.tw","12234");
}
adapter = new SimpleCursorAdapter(this, R.layout.item, cur,
new String[]{"name", "phone", "email","id"},
new int[]{R.id.name1, R.id.phone, R.id.email,R.id.etid}, 0);
lv.setAdapter(adapter); // 設定 Adapter
requery(); // 呼叫自訂方法, 重新查詢及設定按鈕狀態
}
public void add_data(String name,String phone,String email,String id) //由程式新增
{
cv = new ContentValues(4); // 建立含 4 個欄位的 ContentValues物件
cv.put(FROM[0], name); //鍵,值
cv.put(FROM[1], phone);
cv.put(FROM[2], email);
cv.put(FROM[3], id);
db.insert(TB_NAME, null, cv); // 新增1筆記錄
requery(); // 更新 Cursor 內容
}
public void onInsertUpdate(View v) //新增
{
cv = new ContentValues(4); // 建立含 4 個欄位的 ContentValues物件
cv.put(FROM[0], etName.getText().toString()); //鍵,值
cv.put(FROM[1], etPhone.getText().toString());
cv.put(FROM[2], etEmail.getText().toString());
cv.put(FROM[3], etid.getText().toString());
db.insert(TB_NAME, null, cv); // 新增1筆記錄
requery();// 更新 Cursor 內容
}
public void update(View v){ //修改
cv = new ContentValues(4);
cv.put(FROM[0], etName.getText().toString()); //鍵,值
cv.put(FROM[1], etPhone.getText().toString());
cv.put(FROM[2], etEmail.getText().toString());
cv.put(FROM[3], etid.getText().toString() );
db.update(TB_NAME, cv, "_id=" + cur.getString(0), null); // 更新 id 所指的欄位
requery(); // 呼叫自訂方法, 重新查詢及設定按鈕狀態
}
public void onDelete(View v) //刪除
{ // 刪除鈕的On Click事件方法
db.delete(TB_NAME,"_id=" + cur.getString(0), null);
requery();
}
private void requery() //查詢
{ // 重新查詢的自訂方法
cur = db.rawQuery("SELECT * FROM " + TB_NAME, null);
adapter.changeCursor(cur); // 更改 Adapter的Cursor
btUpdate.setEnabled(false); // 停用更新鈕
btDelete.setEnabled(false); // 停用刪除鈕
}
@Override //點選
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
cur.moveToPosition(position); // 移動 Cursor 至使用者選取的項目
// 讀出姓名,電話,Email資料並顯示
etName.setText(cur.getString(cur.getColumnIndex(FROM[0])));
etPhone.setText(cur.getString(cur.getColumnIndex(FROM[1])));
etEmail.setText(cur.getString(cur.getColumnIndex(FROM[2])));
etid.setText(cur.getString(cur.getColumnIndex(FROM[3])));
btUpdate.setEnabled(true); // 啟用更新鈕
btDelete.setEnabled(true); // 啟用刪除鈕
}
}
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener
{
static final String DB_NAME = "HotlineDB";
static final String TB_NAME = "hotlist";
static final String[] FROM = new String[]{"name", "phone", "email","id"};
SQLiteDatabase db;
Cursor cur;
SimpleCursorAdapter adapter;
EditText etName, etPhone, etEmail,etid;
Button btInsert, btUpdate, btDelete;
ListView lv;
ContentValues cv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etPhone = (EditText) findViewById(R.id.etPhone);
etEmail = (EditText) findViewById(R.id.etEmail);
etid = (EditText) findViewById(R.id.etid);
btInsert = (Button) findViewById(R.id.btInsert);
btUpdate = (Button) findViewById(R.id.btUpdate);
btDelete = (Button) findViewById(R.id.btDelete);
lv = (ListView) findViewById(R.id.lv);
lv.setOnItemClickListener(this); // 設定按下事件的監聽器
// 開啟或建立資料庫
db = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
// 建立資料表
String createTable = "CREATE TABLE IF NOT EXISTS " + TB_NAME +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + // 索引欄位
"name VARCHAR(32), " +
"phone VARCHAR(16), " +
"email VARCHAR(64),"+
"id VARCHAR(20))";
db.execSQL(createTable);
cur = db.rawQuery("SELECT * FROM " + TB_NAME, null);
// 若查詢結果是空的則寫入 2 筆測試資料
if (cur.getCount() == 0) //取得資料表列數
{
add_data("Yahoo", "02-12345678", "service@yahoo.com.tw","1234");
add_data("PChome", "02-87654321", "service@pchome.com.tw","12234");
}
adapter = new SimpleCursorAdapter(this, R.layout.item, cur,
new String[]{"name", "phone", "email","id"},
new int[]{R.id.name1, R.id.phone, R.id.email,R.id.etid}, 0);
lv.setAdapter(adapter); // 設定 Adapter
requery(); // 呼叫自訂方法, 重新查詢及設定按鈕狀態
}
public void add_data(String name,String phone,String email,String id) //由程式新增
{
cv = new ContentValues(4); // 建立含 4 個欄位的 ContentValues物件
cv.put(FROM[0], name); //鍵,值
cv.put(FROM[1], phone);
cv.put(FROM[2], email);
cv.put(FROM[3], id);
db.insert(TB_NAME, null, cv); // 新增1筆記錄
requery(); // 更新 Cursor 內容
}
public void onInsertUpdate(View v) //新增
{
cv = new ContentValues(4); // 建立含 4 個欄位的 ContentValues物件
cv.put(FROM[0], etName.getText().toString()); //鍵,值
cv.put(FROM[1], etPhone.getText().toString());
cv.put(FROM[2], etEmail.getText().toString());
cv.put(FROM[3], etid.getText().toString());
db.insert(TB_NAME, null, cv); // 新增1筆記錄
requery();// 更新 Cursor 內容
}
public void update(View v){ //修改
cv = new ContentValues(4);
cv.put(FROM[0], etName.getText().toString()); //鍵,值
cv.put(FROM[1], etPhone.getText().toString());
cv.put(FROM[2], etEmail.getText().toString());
cv.put(FROM[3], etid.getText().toString() );
db.update(TB_NAME, cv, "_id=" + cur.getString(0), null); // 更新 id 所指的欄位
requery(); // 呼叫自訂方法, 重新查詢及設定按鈕狀態
}
public void onDelete(View v) //刪除
{ // 刪除鈕的On Click事件方法
db.delete(TB_NAME,"_id=" + cur.getString(0), null);
requery();
}
private void requery() //查詢
{ // 重新查詢的自訂方法
cur = db.rawQuery("SELECT * FROM " + TB_NAME, null);
adapter.changeCursor(cur); // 更改 Adapter的Cursor
btUpdate.setEnabled(false); // 停用更新鈕
btDelete.setEnabled(false); // 停用刪除鈕
}
@Override //點選
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
cur.moveToPosition(position); // 移動 Cursor 至使用者選取的項目
// 讀出姓名,電話,Email資料並顯示
etName.setText(cur.getString(cur.getColumnIndex(FROM[0])));
etPhone.setText(cur.getString(cur.getColumnIndex(FROM[1])));
etEmail.setText(cur.getString(cur.getColumnIndex(FROM[2])));
etid.setText(cur.getString(cur.getColumnIndex(FROM[3])));
btUpdate.setEnabled(true); // 啟用更新鈕
btDelete.setEnabled(true); // 啟用刪除鈕
}
}
留言
張貼留言