说明:多加一点完善
1.在操作类 DAO 将连接数据库也放到了构造方法中(错误),加删除全部的方法
2.主界面增加了姓名一栏,用layout文件将对话框分离出来
3.删除加确认提示
4.加删除全部按钮
BlackNumber.java
package com.example.chenshuai.test321;/** * Created by chenshuai on 2016/4/16. *///实体类 只负责装载数据public class BlackNumber { private long id; private String name; private String phonenumber; //生成get和set方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } //生成构造方法 public BlackNumber() { } public BlackNumber(String name, String phonenumber) { this.name = name; this.phonenumber = phonenumber; } public BlackNumber(long id,String name,String phonenumber) { this.id = id; this.name = name; this.phonenumber = phonenumber; } //生成tostring方法 @Override public String toString() { return "BlackNumber{" + "id=" + id + ", name='" + name + '\'' + ", phonenumber='" + phonenumber + '\'' + '}'; }}
BlackNumberDAO.java
package com.example.chenshuai.test321;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.util.Log;import java.util.ArrayList;import java.util.List;/** * Created by chenshuai on 2016/4/16. *///数据库操作类public class BlackNumberDAO { private DBHelper dbHelper; //构造方法 初始化 操作工具类DBHelper 连接数据库 public BlackNumberDAO(Context context) { dbHelper = new DBHelper(context); } //增 public void insert(BlackNumber blackNumber) { //连接数据库 SQLiteDatabase sd = dbHelper.getWritableDatabase(); //添加数据 ContentValues cv = new ContentValues(); //要添加数据的键值对集合 cv.put("name",blackNumber.getName()); cv.put("phone_number",blackNumber.getPhonenumber()); //数据添加 sd.insert("black_number",null,cv); //关闭数据库 sd.close(); Log.e("TAG", "添加数据成功"); } //查询 //用List <实体类的实例> 返回数据 public ListgetAll() { //连接数据库 SQLiteDatabase sd = dbHelper.getWritableDatabase(); //List 接口 ArrayList 实现类 List 实体类的实例>bln = new ArrayList (); //查询数据 //连接数据库 //SQLiteDatabase sd = dbHelper.getWritableDatabase(); //返回游标 Cursor cursor = sd.query("black_number", null, null, null, null, null, "_id desc"); //遍历 移动游标,获取数据 while (cursor.moveToNext()) { //构造实体类的实例,放入List bln.add(new BlackNumber(cursor.getLong(0),cursor.getString(1),cursor.getString(2))); } Log.e("TAG", "数据的记录条数" + cursor.getCount()); sd.close(); return bln; } //改 public void update(BlackNumber blackNumber) { //连接数据库 SQLiteDatabase sd = dbHelper.getWritableDatabase(); //要添加数据的键值对集合 ContentValues cv = new ContentValues(); cv.put("name",blackNumber.getName()); cv.put("phone_number",blackNumber.getPhonenumber()); sd.update("black_number",cv,"_id=?",new String[]{String.valueOf(blackNumber.getId())}); sd.close(); Log.e("TAG", "修改数据成功"); } //删 public void delete(long id) { //连接数据库 SQLiteDatabase sd = dbHelper.getWritableDatabase(); //删除数据 sd.delete("black_number", "_id=?", new String[]{String.valueOf(id)}); sd.close(); Log.e("TAG", "删除数据成功"); } //全部删除 public void deleteAll() { //连接数据库 SQLiteDatabase sd = dbHelper.getWritableDatabase(); //删除全部数据 sd.delete("black_number",null,null); sd.close(); Log.e("TAG", "删除数据成功"); }}
activity_activity_black_number.xml
layout_duihuakuang.xml
layout_blacknumber_xianshi.xml
Activity_blackNumber.java
package com.example.chenshuai.test321;import android.app.AlertDialog;import android.app.ListActivity;import android.content.DialogInterface;import android.os.Bundle;import android.view.ContextMenu;import android.view.LayoutInflater;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import java.util.List;public class Activity_blackNumber extends ListActivity { private Listdata; private listviewAdapter lad; private int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_black_number); ListView lv_1 = getListView(); //构建数据 BlackNumberDAO bln = new BlackNumberDAO(Activity_blackNumber.this); data = bln.getAll(); //适配器 lad = new listviewAdapter(); lv_1.setAdapter(lad); //设置上下文菜单 lv_1.setOnCreateContextMenuListener(this); } //创建上下文菜单 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { //添加两个item menu.add(0,1,1,"修改"); menu.add(0, 2, 2, "删除"); //获取长按的适配器信息(删除要用) AdapterView.AdapterContextMenuInfo menuInfo1 = (AdapterView.AdapterContextMenuInfo)menuInfo; position = menuInfo1.position; super.onCreateContextMenu(menu, v, menuInfo); } //给选项加点击事件 @Override public boolean onContextItemSelected(MenuItem item) { final BlackNumber bt = data.get(position); switch (item.getItemId()) { //修改 case 1: LayoutInflater inflater = getLayoutInflater(); final View v = inflater.inflate(R.layout.layout_duihuakuang, null); final EditText et_1 = (EditText)v.findViewById(R.id.et_bt_1); final EditText et_2 = (EditText)v.findViewById(R.id.et_bt_2); //获取默认值 et_1.setText(bt.getName()); et_2.setText(bt.getPhonenumber()); new AlertDialog.Builder(this) .setView(v) .setNegativeButton("取消",null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //获取修改后的值 bt.setName(et_1.getText().toString()); bt.setPhonenumber(et_2.getText().toString()); //执行修改数据库操作 BlackNumberDAO blackNumberDAO = new BlackNumberDAO(Activity_blackNumber.this); blackNumberDAO.update(bt); //更新集合 //BlackNumber bt = data.get(position) //bt和data里面的对象同时指向的是同一个内存地址 //如果bt被修改了,data里面的对象也同时发生变化 //这就是引用类型的特点 //通知刷新列表 lad.notifyDataSetChanged(); Toast.makeText(Activity_blackNumber.this, "修改成功!", Toast.LENGTH_SHORT).show(); } }) .setCancelable(false) .show(); break; //删除 case 2: new AlertDialog.Builder(this) .setMessage("确定要删除吗?") .setNegativeButton("取消",null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //在数据库删除 需要id BlackNumberDAO blackNumberDAO = new BlackNumberDAO(Activity_blackNumber.this); blackNumberDAO.delete(bt.getId()); //刷新界面 //移除集合中的记录 data.remove(position); //通知刷新 lad.notifyDataSetChanged(); Toast.makeText(Activity_blackNumber.this, "删除成功", Toast.LENGTH_SHORT).show(); } }) .setCancelable(false) .show(); break; } return super.onContextItemSelected(item); } //抽象类 要继承并实现抽象方法后才能使用 实现适配器 class listviewAdapter extends BaseAdapter { @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return data.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { //判断可复用的视图是否为空 if (convertView == null) { //获取视图 //1.获取layout文件 用加载器 LayoutInflater layoutInflater = getLayoutInflater(); convertView = layoutInflater.inflate(R.layout.layout_blacknumber_xianshi,null); } //给视图绑定数据 TextView tv_1 = (TextView) convertView.findViewById(R.id.tv_bt_2); TextView tv_2 = (TextView) convertView.findViewById(R.id.tv_bt_3); tv_1.setText(data.get(position).getName()); tv_2.setText(data.get(position).getPhonenumber()); return convertView; } } //添加数据按钮 public void tianjiaonclick(View view) { //1.获取加载器 LayoutInflater layoutInflater = getLayoutInflater(); //2.用加载器加载文件 final View v = layoutInflater.inflate(R.layout.layout_duihuakuang,null); new AlertDialog.Builder(this) .setView(v) .setCancelable(false) .setNegativeButton("取消", null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText et_1 = (EditText)v.findViewById(R.id.et_bt_1); EditText et_2 = (EditText)v.findViewById(R.id.et_bt_2); String name = et_1.getText().toString(); String number = et_2.getText().toString(); //1.构造实体类 参数为输入的电话号码 BlackNumber blackNumber = new BlackNumber(name,number); BlackNumberDAO blackNumberDAO = new BlackNumberDAO(Activity_blackNumber.this); //2.用数据库操作类 DAO 保存数据 blackNumberDAO.insert(blackNumber); //添加数据到集合 //给add添加索引值从上往下加数据 data.add(0,blackNumber); //通知适配器刷新ListView lad.notifyDataSetChanged(); } }) .show(); } //全部删除 public void quanbusconclick(View view) { new AlertDialog.Builder(this) .setMessage("确定要删除吗?") .setNegativeButton("取消",null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //在数据库删除 BlackNumberDAO blackNumberDAO = new BlackNumberDAO(Activity_blackNumber.this); blackNumberDAO.deleteAll(); //刷新界面 //移除集合中的记录 while (position < data.size()) { data.remove(position); } //通知刷新 lad.notifyDataSetChanged(); Toast.makeText(Activity_blackNumber.this, "删除所有数据成功", Toast.LENGTH_SHORT).show(); } }) .setCancelable(false) .show(); }}