添加黑名单

layout文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/lv_1"></ListView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加黑名单"
        android:onClick="add_onClick"/>
</LinearLayout>
View Code

MainActivity代码:

package com.example.my.blacklist;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 com.example.my.blacklist.com.hanqi.blacklist.orm.BlackList;
import com.example.my.blacklist.com.hanqi.blacklist.orm.BlackListDAO;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView lv_1;

    //数据访问对象
    BlackListDAO bld=new BlackListDAO(this);

    //数据集合
    ArrayList<BlackList> alb;

    BLAdapter bla;

    //长按的数据的索引
    int index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        //增加上下文菜单,设置创建菜单的监听器
        lv_1.setOnCreateContextMenuListener(this);

        //获取数据集合
        alb=bld.getAll();

        //显示数据
        //adpter
        bla=new BLAdapter();
        lv_1.setAdapter(bla);

    }

    //重写创建上下文菜单的方法

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.add(0, 1, 1, "修改");
        menu.add(0,2,2,"删除");

        //获取长按的数据信息
        //1.得到菜单信息
        AdapterView.AdapterContextMenuInfo adapterContextMenuInfo=(AdapterView.AdapterContextMenuInfo)menuInfo;

        //2.得到数据在集合中的索引
        index=adapterContextMenuInfo.position;


    }

    //响应菜单点击的方法

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId())
        {
            case 1:
                //修改
                final EditText editText = new EditText(this);
                editText.setHint("输入电话号码");
                editText.setInputType(InputType.TYPE_CLASS_PHONE);

                //构建对话框
                new AlertDialog.Builder(this)
                        .setTitle("修改")
                        .setView(editText)
                        .setCancelable(false)
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //1.得到新数据的实体类
                                //赋值:
                                // 1)传值,复制新的值在传递,值类型
//                              BlackList blackList = new BlackList(alb.get(index).getId(),editText.getText().toString());
                                //2.调用DAO的update()
//                                bld.update(blackList);
//                                alb=bld.getAll();
                                //刷新列表
//                               bla.notifyDataSetChanged();
                                // 2)传址,传递的是内存地址,指向同一个对象,引用类型
                                BlackList blackList=alb.get(index);

                                blackList.setPhoneNumber(editText.getText().toString());
                                //2.调用DAO的update()
                                if (bld.update(blackList)>0)
                                {
                                    Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();

                                    //更新list

                                    //刷新列表
                                    bla.notifyDataSetChanged();
                                }else
                                {
                                    Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                                }


                            }
                        })
                        .show();
                break;
            case 2:
                //删除确认对话框
                new AlertDialog.Builder(this)
                        .setTitle("确认删除")
                        .setMessage("确认将该号码移除黑名单吗?")
                        .setCancelable(false)
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //删除
                                if (bld.delete(alb.get(index).getId())>0) {
                                    Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                    //更新List
                                    //alb = bld.getAll();
                                    alb.remove(index);
                                    //blackList.setId(l);
//                alb.add(0,blackList);
                                    //刷新列表
                                    bla.notifyDataSetChanged();
                                }else
                                {
                                    Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .show();




                break;
        }

        return super.onContextItemSelected(item);
    }

    //BaseAdapter的实现类
    class BLAdapter extends BaseAdapter
    {
        @Override
        public int getCount() {
            return alb.size();
        }

        @Override
        public Object getItem(int position) {
            return alb.get(position);
        }

        @Override
        public long getItemId(int position) {
            return alb.get(position).getId();
        }

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

            //得到数据
            BlackList blackList=alb.get(position);

            //得到视图
            if (convertView == null)
            {
                //构建视图
                convertView =new TextView(MainActivity.this);
            }
            //视图和数据做显示匹配
            TextView textView=(TextView)convertView;

            textView.setTextSize(20);


            textView.setHeight(80);

            textView.setText(blackList.getPhoneNumber());
            return textView;
        }
    }

    //添加
    public void add_onClick(View v)
    {
        //自定义对话框
        final EditText editText = new EditText(this);
        editText.setHint("输入电话号码");
        editText.setInputType(InputType.TYPE_CLASS_PHONE);

        //构建对话框
        new AlertDialog.Builder(this)
                .setTitle("填加黑名单")
                .setView(editText)
                .setCancelable(false)
                .setNegativeButton("取消", null)
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //向数据库保存
                        //1.定义实体类
                        BlackList blackList = new BlackList(editText.getText().toString());

                        //2.通过DAO插入数据
                        long l = new BlackListDAO(MainActivity.this).insert(blackList);
                        if (l > 0) {
                            Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

                            //更新List
                            //alb=bld.getAll();
                            blackList.setId(l);
                            alb.add(0,blackList);
                            //刷新列表
                            bla.notifyDataSetChanged();
                        } else {
                            Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show();
    }
}
View Code

BlackList代码:

package com.example.my.blacklist.com.hanqi.blacklist.orm;

/**
 * Created by My on 2016/6/6.
 */

//实体类
public class BlackList {

    private long id;
    private String phoneNumber;

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

    public BlackList(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}
View Code

BlackListDAO代码:

package com.example.my.blacklist.com.hanqi.blacklist.orm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

/**
 * Created by My on 2016/6/6.
 */

//数据库操作类
public class BlackListDAO {
    private Context context;

    private DBHelper dh;

    private  final String TABEL_NAME ="t_blacklist";

    public BlackListDAO(Context context)
    {
        //this.context=context;
        dh=new DBHelper(context);
    }
    ////传入参数: 实体类的实例
    public long insert(BlackList blackList) {
        long rtn = 0;

        //连接数据库
        SQLiteDatabase sd=dh.getWritableDatabase();
        //执行insert语句
        //insert into t_blacklist (phone_number) values ()
        ContentValues  cv =new ContentValues();
        cv.put("phone_number",blackList.getPhoneNumber());

        rtn = sd.insert("t_blacklist",null,cv);

        sd.close();

        return rtn;
    }

    //
    public int delete(long id) {
        int rtn = 0;

        //连接数据库
        SQLiteDatabase sd=dh.getWritableDatabase();
        //delete from t_blacklist where _id = ?

        rtn=sd.delete(TABEL_NAME,"_id=?",new String[]{id+""});

        sd.close();

        return rtn;
    }

    //
    public int update(BlackList blackList) {
        int rtn = 0;
        //连接数据库
        SQLiteDatabase sd=dh.getWritableDatabase();

        //update t_blacklist set phone_number = ? where _id = ?
        ContentValues cv=new ContentValues();
        cv.put("phone_number",blackList.getPhoneNumber());
        sd.update(TABEL_NAME,cv,"_id=?", new String[]{blackList.getId()+""});

        return rtn;
    }

    ////返回查询结果
    public ArrayList<BlackList> getAll()
    {
        ArrayList<BlackList> blackLists = new ArrayList<>();

        //连接数据库
        SQLiteDatabase sd=dh.getWritableDatabase();

        //select * from t_Blacklist

        //查询之后得到游标结果集

        Cursor cursor=sd.query(TABEL_NAME, null, null, null, null, null, "_id desc");

        //遍历结果集
        while (cursor.moveToNext())
        {


        //1.把数据转成实体类的实例

            BlackList blackList = new BlackList(cursor.getLong(0),cursor.getString(1));

        //2.把实例放在集合里
            blackLists.add(blackList);
        }
        cursor.close();

        sd.close();

        //返回这个集合
        return blackLists;


    }
}
View Code

DBHelper代码:

package com.example.my.blacklist.com.hanqi.blacklist.orm;

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

/**
 * Created by My on 2016/6/7.
 */
public class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context)
    {
        super(context,"balcklist.db",null,1);
    }
    public  void  onCreate(SQLiteDatabase db)
    {
        //1.执行创建数据库的语句
       String sql = "CREATE TABLE t_blacklist " +
            "(_id  INTEGER NOT NULL," +
            "phone_number  VARCHAR(20) NOT NULL,"
            +"PRIMARY KEY (\"_id\"))";
        db.execSQL(sql);
        Log.e("TAG", "表创建成功");

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

    }

}
View Code

 

posted on 2016-06-08 16:35  beens  阅读(186)  评论(0编辑  收藏  举报

导航