sqlite數據庫的基本增刪改查操做MainActivity類代碼

public class MainActivity extends Activity {sql

 private ListView listview;
 private SimpleAdapter adapter;
 private Sqlite_operate_utils sql;數據庫

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);ide

  this.listview = (ListView) this.findViewById(R.id.listview);
  sql = new Sqlite_operate_utils();
  // 註冊菜單
  registerForContextMenu(listview);
  adapter();ui

 }this

 // 建立一個選擇菜單
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.add_menu, menu);
  return super.onCreateOptionsMenu(menu);
 }sqlite

 // 建立 一個上下文菜單
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  // 設置選中的 名字 爲 菜單標題
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
  Map<String, String> info_map = (Map<String, String>) adapter
    .getItem(info.position);事件

  menu.setHeaderTitle(info_map.get("s_sex"));
  getMenuInflater().inflate(R.menu.list_menu, menu);
 }get

 // 山下文 點擊事件
 @Override
 public boolean onContextItemSelected(MenuItem item) {
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
    .getMenuInfo();
  Map<String, String> item_map = (Map<String, String>) adapter
    .getItem(info.position);
  // 拿到 選中的id值
  final int id = Integer.parseInt(item_map.get("s_id"));
  // 選擇監聽
  switch (item.getItemId()) {
  case R.id.list_delete:// 刪除信息 點擊事件
   // 彈出提示對話框
   AlertDialog.Builder builder = dialog(R.drawable.ic_launcher, "提示:");
   builder.setMessage("肯定要刪除");
   builder.setPositiveButton("肯定", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     String execute_content = "delete from studentinfo where s_id = ?";
     Object[] bindArgs = { id };
     boolean result = sql.executeData(execute_content, bindArgs);it

     if (result) {
      Toast.makeText(MainActivity.this, "刪除信息成功",
        Toast.LENGTH_SHORT).show();
      adapter();// 更新listview
     } else {
      Toast.makeText(MainActivity.this, "刪除失敗",
        Toast.LENGTH_SHORT).show();
     }
    }
   });
   builder.show();
   break;
  case R.id.list_change:
   // 按下修改信息 菜單 時 彈出 修改 編輯框
   AlertDialog.Builder builder2 = dialog(R.drawable.ic_launcher, "提示:");io

   View view = getLayoutInflater().inflate(R.layout.editdata_activity,
     null);
   final EditText edit_name = (EditText) view
     .findViewById(R.id.edit_name);
   final EditText edit_sex = (EditText) view
     .findViewById(R.id.edit_sex);
   final EditText edit_age = (EditText) view
     .findViewById(R.id.edit_age);
   builder2.setView(view);

   //把選擇的列表信息 填充到 要修改的 編輯框裏
   String content = "select * from studentinfo where s_id = ?";
   String[] condition = {id + ""};
   Cursor cursor_student = sql.sqlite_select(content, condition);//查詢出選中的 信息
   cursor_student.moveToFirst();//把遊標 移到第一個位置
   
   edit_name.setText(cursor_student.getString(cursor_student.getColumnIndex("s_sex")));
   edit_sex.setText(cursor_student.getString(cursor_student.getColumnIndex("s_age")));
   edit_age.setText(cursor_student.getString(cursor_student.getColumnIndex("s_score")));
   //關閉 遊標
   cursor_student.close();
   //肯定 修改 信息 按鈕 事件 監聽
   builder2.setPositiveButton("肯定", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     String name = edit_name.getText().toString();
     String sex = edit_sex.getText().toString();
     String age = edit_age.getText().toString();
     String execute_content = "update studentinfo set s_sex =?,s_age =?,s_score =? where s_id =?";
     Object[] bindArgs = {name,sex,age,id};
     // 把修改的數據 從新寫入數據庫
     boolean result = sql.executeData(execute_content, bindArgs);
     if (result) {
      Toast.makeText(MainActivity.this, "修改信息成功",
        Toast.LENGTH_SHORT).show();
      adapter();// 更新listview
     } else {
      Toast.makeText(MainActivity.this, "修改失敗",
        Toast.LENGTH_SHORT).show();
     }
    }
   });
   builder2.show();
   break;
  }

  return super.onContextItemSelected(item);
 }

 // 添加學生 數據
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.adddata_menu:
   // 按下添加信息 菜單 時 彈出 添加 編輯框
   AlertDialog.Builder builder = dialog(R.drawable.ic_launcher, "提示:");

   View view = getLayoutInflater().inflate(R.layout.editdata_activity,
     null);
   final EditText edit_name = (EditText) view
     .findViewById(R.id.edit_name);
   final EditText edit_sex = (EditText) view
     .findViewById(R.id.edit_sex);
   final EditText edit_age = (EditText) view
     .findViewById(R.id.edit_age);
   builder.setView(view);

   builder.setPositiveButton("肯定", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
     String name = edit_name.getText().toString();
     String sex = edit_sex.getText().toString();
     String age = edit_age.getText().toString();
     // SQL語句 添加 數據
     String execute_content = "insert into studentinfo(s_sex,s_age,s_score) values(?,?,?)";
     Object[] bindArgs = { name, sex, age };
     // 把添加 的數據 寫入 數據庫
     boolean result = sql.executeData(execute_content, bindArgs);
     if (result) {
      Toast.makeText(MainActivity.this, "添加信息成功",
        Toast.LENGTH_SHORT).show();
      adapter();// 更新listview
     } else {
      Toast.makeText(MainActivity.this, "添加失敗",
        Toast.LENGTH_SHORT).show();
     }
    }
   });
   builder.show();
   break;
  }
  return super.onOptionsItemSelected(item);
 }

 // Dialog 框
 public AlertDialog.Builder dialog(int icon, String title) {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setTitle(title);
  builder.setIcon(icon);

  builder.setNeutralButton("取消", null);
  return builder;
 }

 // 設置 適配器 的方法
 public void adapter() {
  String content = "select * from studentinfo";
  List<Map<String, String>> data = sql.sqlite_selectlist(content, null);
  adapter = new SimpleAdapter(this, data, R.layout.item_activity,
    new String[] { "s_sex", "s_age", "s_score" }, new int[] {
      R.id.item_name, R.id.item_sex, R.id.item_age });
  listview.setAdapter(adapter);
 }

}

相關文章
相關標籤/搜索