该工具类用于管理数据库创建、连接及版本管理
package com.androidlearning.blacklist_110;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
/**
* 数据库版本创建工具类
* @author 刘昌兴
* @since 2022/4/10 13:25
*/
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "atguigu.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("Tag","DBHelper onCreate");
db.execSQL("create table blacklist(_id integer primary key autoincrement,number varchar)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
创建一个BlackNumber黑名单电话实体类
package com.androidlearning.blacklist_110;
/**
* 黑名单号码实体类
* @author 刘昌兴
* @since 2022/4/10 13:31
*/
public class BlackNumber {
private int id;
private String number;
public BlackNumber(int id, String number) {
this.id = id;
this.number = number;
}
public BlackNumber() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "BlackNumber{" +
"id=" + id +
", number='" + number + '\'' +
'}';
}
}
package com.androidlearning.blacklist_110;
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;
/**
* 黑名单数据组操作Dao层
* @author 刘昌兴
* @since 2022/4/10 13:35
*/
public class BlackListDao {
private DBHelper dbHelper;
public BlackListDao(Context context){
dbHelper=new DBHelper(context);
}
/**
* 增加号码
* @author 刘昌兴
* @since 2022/4/10 13:36
* @param blackNumber
*/
public void addNumber(BlackNumber blackNumber){
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("number",blackNumber.getNumber());
long id = readableDatabase.insert("blacklist", null, contentValues);
blackNumber.setId((int) id);
Log.i("TAG", String.valueOf(id));
readableDatabase.close();
}
/**
* 删除号码
* @author 刘昌兴
* @since 2022/4/10 13:37
* @param id
*/
public void deleteNumberById(int id){
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
int row = readableDatabase.delete("blacklist", "_id=?", new String[]{String.valueOf(id)});
Log.i("TAG", String.valueOf(row));
readableDatabase.close();
}
/**
* 更新号码
* @author 刘昌兴
* @since 2022/4/10 13:37
* @param blackNumber
*/
public void updateNumber(BlackNumber blackNumber){
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("number",blackNumber.getNumber());
int row = readableDatabase.update("blacklist", contentValues, "_id=" + blackNumber.getId(), null);
Log.i("TAG", String.valueOf(row));
readableDatabase.close();
}
/**
* 查询黑名单
* @author 刘昌兴
* @since 2022/4/10 13:38
* @param
* @return
*/
public List<BlackNumber> getBlackList(){
List<BlackNumber> blackNumberList=new ArrayList<>();
SQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
Cursor cursor = readableDatabase.query("blacklist", null, null, null, null, null, "_id desc");
while (cursor.moveToNext()){
blackNumberList.add(new BlackNumber(cursor.getInt(0),cursor.getString(1)));
}
cursor.close();
readableDatabase.close();
return blackNumberList;
}
}
这里我们使用继承ListActivity的方式来实现列表布局
package com.androidlearning.blacklist_110;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
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 androidx.annotation.NonNull;
import java.util.List;
/**
* 继承ListActivity实现布局
* @author 刘昌兴
* @since 2022/4/10 19:49
*/
public class MainListActivity extends ListActivity {
private ListView blackListView;
private List<BlackNumber> blackNumberList;
private BlackListDao blackListDao;
private BlackListAdapter blackListAdapter;
//上下文菜单的列表item位置
private int positon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_main);
//获取继承的ListView
blackListView=getListView();
blackListDao=new BlackListDao(this);
blackNumberList=blackListDao.getBlackList();
blackListAdapter=new BlackListAdapter();
blackListView.setAdapter(blackListAdapter);
//设置上下文菜单
blackListView.setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0,1,0,"更新");
menu.add(0,2,0,"删除");
//从这个对象中我们可以知道是我们长按了那个item
AdapterView.AdapterContextMenuInfo adapterContextMenuInfo= (AdapterView.AdapterContextMenuInfo) menuInfo;
//获得上下文菜单的item的位置
positon=adapterContextMenuInfo.position;
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
BlackNumber blackNumber=blackNumberList.get(positon);
switch (item.getItemId()){
case 1:
updateNumber(blackNumber);
break;
case 2:
//从数据库中删除
blackListDao.deleteNumberById(blackNumber.getId());
//从列表中删除
blackNumberList.remove(positon);
//通知列表更新
blackListAdapter.notifyDataSetChanged();
Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();
}
return super.onContextItemSelected(item);
}
/**
* 更新黑名单号码
* @author 刘昌兴
* @since 2022/4/10 17:01
* @param blackNumber
* @return
*/
private void updateNumber(BlackNumber blackNumber) {
EditText editText=new EditText(this);
//回显电话号码
editText.setHint(blackNumberList.get(positon).getNumber());
new AlertDialog.Builder(this)
.setTitle("更新黑名单")
.setView(editText)
.setPositiveButton("更新", (dialog, which) -> {
//将新号码设置到对象中
blackNumber.setNumber(editText.getText().toString());
//在数据库中更新
blackListDao.updateNumber(blackNumber);
//通知更新
blackListAdapter.notifyDataSetChanged();
Toast.makeText(MainListActivity.this,"更新成功",Toast.LENGTH_SHORT).show();
})
.setNegativeButton("取消",null)
.show();
}
//添加黑名单回调方法
public void add(View v){
EditText editText=new EditText(this);
editText.setHint("电话号码");
new AlertDialog.Builder(this)
.setTitle("添加黑名单")
.setView(editText)
.setPositiveButton("添加", (dialog, which) -> {
BlackNumber blackNumber=new BlackNumber(-1,editText.getText().toString());
//在数据库中添加
blackListDao.addNumber(blackNumber);
//在列表中添加
blackNumberList.add(0,blackNumber);
//通知更新
blackListAdapter.notifyDataSetChanged();
Toast.makeText(MainListActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
})
.setNegativeButton("取消",null)
.show();
}
/**
* 黑名单适配器
* @author 刘昌兴
* @since 2022/4/10 15:00
*/
class BlackListAdapter extends BaseAdapter{
@Override
public int getCount() {
return blackNumberList.size();
}
@Override
public Object getItem(int position) {
return blackNumberList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=View.inflate(MainListActivity.this, android.R.layout.simple_list_item_1,null);
}
BlackNumber blackNumber=blackNumberList.get(position);
TextView textView=convertView.findViewById(android.R.id.text1);
textView.setText(String.valueOf(blackNumber.getNumber()));
return convertView;
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="没有号码"
android:id="@android:id/empty"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="add"/>
</LinearLayout>