Android——黑名单管理

 

Android——黑名单管理_第1张图片

DBHelper.java

package com.example.chenshuai.test;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by chenshuai on 2016/4/15.
 */
public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, "blacklist.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table black_number (_id integer primary key autoincrement,phone_number varchar)");

        Log.e("TAG","建表完成" );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

BlackNumber.java

package com.example.chenshuai.test;

/**
 * Created by chenshuai on 2016/4/15.
 */
//实体类 只负责装载数据
public class BlackNumber {

    private long id;
    private String phoneNumber;

    //生成get和set方法


    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(long id, String phoneNumber) {
        this.id = id;
        this.phoneNumber = phoneNumber;
    }

    public BlackNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    @Override
    public String toString() {
        return "BlackNumber{" +
                "id=" + id +
                ", phoneNumber='" + phoneNumber + '\'' +
                '}';
    }
}

BlackNumberDAO.java

package com.example.chenshuai.test;

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/15.
 */
//数据操作类
public class BlackNumberDAO {

    private DBHelper dbHelper;
    //构造方法 初始化
    public BlackNumberDAO(Context context){

        //初始化工具类
        dbHelper = new DBHelper(context);
    }

    //增  直接将实体类传进来
    public void insert(BlackNumber blackNumber)
    {
        //获取数据库连接
        SQLiteDatabase sd = dbHelper.getWritableDatabase();

        //要添加数据的键值对集合
        ContentValues contentValues = new ContentValues();
        contentValues.put("phone_number", blackNumber.getPhoneNumber());

        //数据添加
        sd.insert("black_number", null, contentValues);

        //关闭数据库
        sd.close();

        Log.e("TAG", "添加数据成功");
    }


    //查询
    //用List<实体类的实例>返回数据
    public List getAll()
    {
        //List 接口 ArrayList 实现类
        List rtn = new ArrayList();

        //查询数据
        //获取数据库连接
        SQLiteDatabase sd = dbHelper.getWritableDatabase();

        //返回游标
        Cursor cursor = sd.query("black_number", null, null, null, null, null, "_id desc");

        //遍历 移动游标,获取数据
        while(cursor.moveToNext())
        {
            //构造实体类的实例,放入List
            rtn.add(new BlackNumber(cursor.getLong(0),cursor.getString(1)));
        }
        Log.e("TAG", "数据的记录条数" + cursor.getCount());

        sd.close();

        return rtn;
    }

    //修改
    public void update(BlackNumber blackNumber)
    {
        //获取数据库连接
        SQLiteDatabase sd = dbHelper.getWritableDatabase();

        //要添加数据的键值对集合
        ContentValues contentValues = new ContentValues();
        contentValues.put("phone_number", blackNumber.getPhoneNumber());

        //数据修改
        sd.update("black_number",contentValues,"_id=?",new String[]{String.valueOf(blackNumber.getId())});
        //sd.update("black_number",contentValues,"_id=?",new String[]{blackNumber.getId().toString()});

        //关闭数据库
        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", "删除数据成功");

    }

}
activity_activityhhh1.xml
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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.chenshuai.test.Activityhhh1"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@android:id/list">
    ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="还没有一个黑名单"
        android:textSize="30dp"
        android:gravity="center"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="添加"
        android:onClick="bt_tianjiasjonclick"/>

LinearLayout>

Activityhhh1.java

package com.example.chenshuai.test;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
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 java.util.List;

public class Activityhhh1 extends ListActivity {

    private ListView lv_1;
    private List data;
    private BlackListAdapter bla;
    private int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activityhhh1);

        //lv_1 = (ListView)findViewById(R.id.lv_1);
        lv_1 = getListView();

        //获取数据集合
        BlackNumberDAO bn = new BlackNumberDAO(this);

        data = bn.getAll();

        //设置适配器
        bla = new BlackListAdapter();
        lv_1.setAdapter(bla);
        //设置上下文菜单
        lv_1.setOnCreateContextMenuListener(this);
    }

    //创建上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        //添加两个item
        menu.add(0,0,0,"修改");
        menu.add(0,1,1,"删除");

        //获取长按的适配器信息
        AdapterView.AdapterContextMenuInfo mninfo = (AdapterView.AdapterContextMenuInfo)menuInfo;

        position = mninfo.position;
    }

    //加选项监听
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        final BlackNumber bt = data.get(position);
        switch (item.getItemId())
        {
            //修改
            case 0:
                //弹出对话框
                //定义文本框

                final EditText et = new EditText(this);
                et.setInputType(InputType.TYPE_CLASS_PHONE);
                et.setText(bt.getPhoneNumber());

                new AlertDialog.Builder(this)
                        .setTitle("修改黑名单")
                        .setView(et)
                        .setNeutralButton("取消",null)
                        .setPositiveButton("修改", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                //获取修改后的值
                                bt.setPhoneNumber(et.getText().toString());

                                //执行修改数据库操作
                                BlackNumberDAO bnd = new BlackNumberDAO(Activityhhh1.this);

                                bnd.update(bt);

                                //更新集合
                                //BlackNumber bt = data.get(position)
                                //bt和data里面的对象同时指向的是同一个内存地址
                                //如果bt被修改了,data里面的对象也同时发生变化
                                //这就是引用类型的特点

                                //通知刷新列表
                                bla.notifyDataSetChanged();

                                Toast.makeText(Activityhhh1.this, "修改成功!", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();

                break;

            //删除
            case 1:

                new AlertDialog.Builder(this)
                        .setMessage("确定要删除吗?")
                        .setNeutralButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                                BlackNumberDAO bd = new BlackNumberDAO(Activityhhh1.this);

                                //在数据库删除 需要id
                                bd.delete(bt.getId());

                                //刷新界面
                                //移除集合中的记录
                                data.remove(position);
                                //通知刷新
                                bla.notifyDataSetChanged();

                                Toast.makeText(Activityhhh1.this, "删除成功", Toast.LENGTH_SHORT).show();

                            }
                        })
                        .show();

                break;
        }
        return super.onContextItemSelected(item);
    }

    //列表适配器
    class BlackListAdapter 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) {

            //获取类的实例,获取id
            return data.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //判断可复用的视图是否为空
            if (convertView ==null)
            {
                //获取视图
                //1.获取layout文件  用加载器
               convertView = View.inflate(Activityhhh1.this,android.R.layout.simple_list_item_1,null);

            }

            //给视图绑定数据
            TextView tv = (TextView)convertView.findViewById(android.R.id.text1);

            tv.setText(data.get(position).getPhoneNumber());

            return convertView;
        }
    }
    public void bt_tianjiasjonclick(View view)
    {
        final EditText editText = new EditText(this);
        editText.setHint("输入黑名单号码");
        //editText.setLayoutParams(new ListView.LayoutParams());
        editText.setInputType(InputType.TYPE_CLASS_PHONE);
        //构造自定义对话框
        new AlertDialog.Builder(this)
                .setTitle("添加黑名单")
                .setView(editText)
                .setNegativeButton("取消",null)
                .setPositiveButton("添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //1.构造实体类 参数为输入的电话号码
                        BlackNumber bn = new BlackNumber(editText.getText().toString());

                        //2.用数据库操作类 DAO 保存数据
                        BlackNumberDAO blackNumberDAO = new BlackNumberDAO(Activityhhh1.this);

                        blackNumberDAO.insert(bn);

                        //添加数据到集合
                        //给add添加索引值从上往下加数据
                        data.add(0,bn);

                        //通知适配器刷新ListView
                        bla.notifyDataSetChanged();


                        Toast.makeText(Activityhhh1.this, "数据保存成功", Toast.LENGTH_SHORT).show();
                    }
                })

                .setCancelable(false)
                .show();
  }
}

 

 

 

你可能感兴趣的:(Android——黑名单管理)