效果图:
1 、创建class:BlackNumberDBHelper 黑名单数据库的DBHelp
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
*
* @author Administrator
* 黑名单数据库的DBHelp
*/
public class BlackNumberDBHelper extends SQLiteOpenHelper{
public BlackNumberDBHelper(Context context) {
super(context, "blacknumber.db", null, 1);
}
//第一次创建数据库的时候执行
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE blacknumber (_id integer primary key autoincrement, number varchar(20))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.iponeguardian.db.BlackNumberDBHelper;
/**
*
* @author Administrator
* 黑名单数据库的db操作
*/
public class BlackNumberDao {
private Context context; //上下文
private Cursor cursor; //游标
private SQLiteDatabase db;
private String sql ; //sql语句
private BlackNumberDBHelper dbHelper;
public BlackNumberDao(Context context) {
this.context = context;
dbHelper = new BlackNumberDBHelper(context);
}
/**
* 查找全部
*/
public List getAllNumbers(){
db = dbHelper.getReadableDatabase();
List number_list = new ArrayList(); //存放所有的电话号码
if(db.isOpen()){
sql = "SELECT * FROM BLACKNUMBER";
cursor = db.rawQuery(sql, null); //得到一组游标的结果
while(cursor.moveToNext()){ //遍历游标
String number_all = cursor.getString(1);
number_list.add(number_all);
}
cursor.close();
db.close();
}
return number_list;
}
/**
* 查询的方法
* @param number
* @return
*/
public boolean find (String number){
boolean result = false;
db = dbHelper.getReadableDatabase();
if(db.isOpen()){
sql = "select number from blacknumber where number = ?";
cursor = db.rawQuery(sql, new String[]{number});
if(cursor.moveToNext()){
result = true;
}else{
result = false;
}
cursor.close();
db.close();
}
return result ;
}
/**
* 添加的方法
*/
public void add (String number){
if(find(number)){
return;
}
db = dbHelper.getWritableDatabase();
if(db.isOpen()){
String sql = "insert into blacknumber (number) values (?)";
db.execSQL(sql,new Object[]{number});
db.close();
}
}
/**
* 删除
*/
public void delete(String number){
db = dbHelper.getWritableDatabase();
if(db.isOpen()){
sql = "DELETE FORM BLACKNUMBER WHERE NUMBER=?";
db.execSQL(sql,new Object[]{number});
db.close();
}
}
/**
* 修改更新lodNumber 旧的号码 newNumber新的号码
*/
public void update(String oldNumber, String newNumber){
db = dbHelper.getWritableDatabase();
if(db.isOpen()){
sql = "update BLACKNUMBER SET NUMBER=? WHERE NUMBER = ?";
db.execSQL(sql,new Object[]{newNumber,oldNumber});
db.close();
}
}
}
3、创建用于管理黑名单的布局文件activity_call_sms.xml:
package com.iponeguardian.ui;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.example.iponeguardian.R;
import com.iponeguardian.dao.BlackNumberDao;
/**
* 通讯位置之 黑名单管理的activity
* @author Administrator
*
*/
public class CallSmsActivity extends Activity implements OnClickListener {
private Button btn_add_black_number; //添加黑名单按钮
private ListView listView_callsms; //显示黑名单信息
private BlackNumberDao numberDao; //黑名单数据库
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_sms);
numberDao = new BlackNumberDao(this);
//获得引用
btn_add_black_number = (Button) findViewById(R.id.btn_add_black_number);
listView_callsms = (ListView)findViewById(R.id.listView_callsms);
//绑定侦听事件
btn_add_black_number.setOnClickListener(this);
//调用数据库getAll方法得到所有的黑名单数据,并存在List集合中
numbers = numberDao.getAllNumbers();
for(int i=0;i
}
ArrayAdapter
//为显示黑名单的list设置Adapter
listView_callsms.setAdapter(adapter);
}
@Override
public void onClick(View v) {
//弹出对话框用来添加黑名单,传进去的上下文是CallSmsActivity.this 因为这个对话框挂载在Activity里边
AlertDialog.Builder builder = new Builder(CallSmsActivity.this);
builder.setTitle("添加黑名单");
final EditText etitText = new EditText(CallSmsActivity.this);
//设置它的文本类型只能是数字
etitText.setInputType(InputType.TYPE_CLASS_PHONE);
builder.setView(etitText);
builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//得到输入框中输入的类容
String number = etitText.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(getApplicationContext(), "添加黑名单号码不能为空", 1).show();
return;
}else{
//调用数据库添加方法
numberDao.add(number);
//通知ListView更新数据
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
}
二、 长按ListView列表弹出”更新黑名单“和删除黑名单、listView时实更新
1 长按ListView列表弹出”更新黑名单“和删除黑名单,重写onCreateContextMenu和onContextItemSelected方法
//listView设置的上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.content_menu, menu); //content_menu是配置文件内容
}
配置文件content_menu:
//菜单的点击事件,通过item参数得到当前点击的menu信息
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();
int id = (int)info.id; //当前条目的id
String number = numbers.get(id); //得到list要删除的内容
switch (item.getItemId()) {
case R.id.update_number: //更新的按钮(菜单文件放在menu中的content_menu.xml)
updateNumner(number);
break;
case R.id.delete_number: //删除 的按钮
numberDao.delete(number); //执行数据库的删除操作
numbers = numberDao.getAllNumbers(); //重新获取最新的黑名单
adapter.notifyDataSetChanged(); //通知listView更新数据
break;
}
return super.onContextItemSelected(item);
}
/**
* 更新黑名单的方法
* @param number
*/
private void updateNumner(final String oldNumber) {
AlertDialog.Builder builder = new Builder(CallSmsActivity.this);
builder.setTitle("更改黑名单号码");
final EditText etitText = new EditText(CallSmsActivity.this);
//设置它的文本类型只能是数字
etitText.setInputType(InputType.TYPE_CLASS_PHONE);
builder.setView(etitText);
builder.setPositiveButton("更改", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//得到输入框中输入的新号码
String newNumber = etitText.getText().toString().trim();
if(TextUtils.isEmpty(newNumber)){
Toast.makeText(getApplicationContext(), "添加黑名单号码不能为空", 1).show();
return;
}else{
//调用数据库更改方法
numberDao.update(oldNumber, newNumber);
//调用数据库getAll方法得到所有的黑名单数据,并存在List集合中
numbers = numberDao.getAllNumbers();
//通知ListView更新数据,只更新发生改变的数据
adapter.notifyDataSetChanged();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}