黑名单管理代码总结

Posted 宫崎天川

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了黑名单管理代码总结相关的知识,希望对你有一定的参考价值。

1.界面代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.administrator.blacklist.MainActivity">
 9 
10     <ListView
11         android:layout_width="match_parent"
12         android:layout_height="0dp"
13         android:layout_weight="1"
14         android:id="@+id/lv_1">
15     </ListView>
16     <Button
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:text="添加黑名单"
20         android:onClick="add_onclick"/>
21 </LinearLayout>

2.实体类代码

 1 package com.example.administrator.blacklist.com.hanqi.blacklist;
 2 
 3 /**
 4  * Created by Administrator on 2016/06/06.
 5  */
 6 
 7 //实体类
 8 public class BlackList {
 9     private long id;
10     private String phoneNumber;
11     public long getId() {
12         return id;
13     }
14     public void setId(long id) {
15         this.id = id;
16     }
17     public String getPhoneNumber() {
18         return phoneNumber;
19     }
20     public void setPhoneNumber(String phoneNumber) {
21         this.phoneNumber = phoneNumber;
22     }
23     public BlackList(long id, String phoneNumber) {
24         this.id = id;
25         this.phoneNumber = phoneNumber;
26     }
27     public BlackList(String phoneNumber) {
28         this.phoneNumber = phoneNumber;
29     }
30 }

3.数据库代码

 1 package com.example.administrator.blacklist.com.hanqi.blacklist;
 2 import android.content.Context;
 3 import android.database.sqlite.SQLiteDatabase;
 4 import android.database.sqlite.SQLiteOpenHelper;
 5 import android.util.Log;
 6 /**
 7  * Created by Administrator on 2016/06/07.
 8  */
 9 public class DBHelper extends SQLiteOpenHelper {
10     public DBHelper(Context context) {
11         super(context, "blacklist.db", null, 1);
12     }
13     @Override
14     public void onCreate(SQLiteDatabase db) {
15         String sql = "CREATE TABLE t_blacklist " +
16                 "(_id  INTEGER NOT NULL," +
17                 "phone_number  VARCHAR(20) NOT NULL,"
18                 +"PRIMARY KEY (\\"_id\\"))";
19         db.execSQL(sql);
20         Log.e("TAG", "表创建成功");
21     }
22     @Override
23     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
24     }
25 }

4.数据库操作代码【增删改查】

 1 package com.example.administrator.blacklist.com.hanqi.blacklist;
 2 import android.content.ContentValues;
 3 import android.content.Context;
 4 import android.database.Cursor;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import java.util.ArrayList;
 7 /**
 8  * Created by Administrator on 2016/06/06.
 9  */
10 
11 //数据库操作类
12 public class BlackListDAO {
13     private Context context;
14     private DBHelper dbHelper;
15     private final String TABLE_NAME = "t_blacklist";
16     public BlackListDAO(Context context)
17     {
18 //        this.context = context;
19         dbHelper=new DBHelper(context);
20     }
21     //22     //传入参数:实体类的实例
23 public long insert(BlackList blackList)
24 {
25     long rtn = 0;
26     //连接数据库
27     SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
28     //执行insert语句
29     //insert into t_balcklist(phone_number) values()
30     ContentValues contentValues = new ContentValues();
31     contentValues.put("phone_number", blackList.getPhoneNumber());
32     rtn = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
33     sqLiteDatabase.close();
34     return rtn;
35 }
36     //
37 public int delete(long id)
38 {
39     SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
40     int rtn = 0;
41     // delete from t_blacklist where _id=?
42     rtn = sqLiteDatabase.delete(TABLE_NAME,"_id=?",new String[]{id+""});
43     sqLiteDatabase.close();
44     return rtn;
45 }
46     //
47 public int update(BlackList blackList)
48 {
49     SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
50     ContentValues cv = new ContentValues();
51     cv.put("phone_number",blackList.getPhoneNumber().toString());
52     int rtn = 0;
53 //update语句 t_blacklist set phone_number = ? where _id = ?
54     rtn = sqLiteDatabase.update(TABLE_NAME,cv,"_id=?",new String[]{blackList.getId()+""});
55     sqLiteDatabase.close();
56     return rtn;
57 }
58     //59     //返回查询
60     public ArrayList<BlackList> getAll()
61     {
62         //连接数据库
63         SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
64         ArrayList<BlackList>blackLists = new ArrayList<>();
65         //select * from t_blacklist
66         //查询之后得到游标结果集
67         Cursor cursor = sqLiteDatabase.query(TABLE_NAME, null, null, null, null, null,"_id desc");
68         //遍历结果集
69         //1.把数据转成实体类的实例
70         while(cursor.moveToNext()) {
71             BlackList blacklist = new BlackList(cursor.getLong(0), cursor.getString(1));
72             //2.放在集合里,返回这个集合
73             blackLists.add(blacklist);
74         }
75         cursor.close();
76         sqLiteDatabase.close();
77         return blackLists;
78     }
79 }

5.操作代码

  1 package com.example.administrator.myapplication;
  2 import android.app.AlertDialog;
  3 import android.content.DialogInterface;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.text.InputType;
  7 import android.view.ContextMenu;
  8 import android.view.LayoutInflater;
  9 import android.view.MenuItem;
 10 import android.view.View;
 11 import android.view.ViewGroup;
 12 import android.widget.AdapterView;
 13 import android.widget.BaseAdapter;
 14 import android.widget.EditText;
 15 import android.widget.ListView;
 16 import android.widget.Switch;
 17 import android.widget.TextView;
 18 import android.widget.Toast;
 19 import com.example.administrator.myapplication.com.hanqi.blacklist.BlackList;
 20 import com.example.administrator.myapplication.com.hanqi.blacklist.BlackListDAO;
 21 import java.lang.reflect.Type;
 22 import java.util.ArrayList;
 23 import java.util.List;
 24 import java.util.Map;
 25 import java.util.Objects;
 26 
 27 public class MainActivity extends AppCompatActivity {
 28     ListView lv_1;
 29     //数据访问对象
 30     BlackListDAO blackListDAO = new BlackListDAO(this);
 31     //数据集合
 32     ArrayList<BlackList> alb;
 33     BLAdapter blAdapter;
 34     //长按数据的索引
 35     int index;
 36     @Override
 37     protected void onCreate(Bundle savedInstanceState) {
 38         super.onCreate(savedInstanceState);
 39         setContentView(R.layout.activity_main);
 40 
 41         lv_1=(ListView)findViewById(R.id.lv_1);
 42         //增加上下文菜单 设置创建菜单的监听器
 43         lv_1.setOnCreateContextMenuListener(this);
 44         //获取数据集合
 45         alb = blackListDAO.getAll();
 46         //显示数据
 47         lv_1.setAdapter(new BLAdapter());
 48     }
 49     //重写创建上下文菜单的方法
 50     @Override
 51     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
 52         super.onCreateContextMenu(menu, v, menuInfo);
 53         menu.add(0, 1, 1, "修改");
 54         menu.add(0, 2, 2, "删除");
 55         //获取长按的数据信息
 56         //1-得到菜单信息
 57         AdapterView.AdapterContextMenuInfo acmi=(AdapterView.AdapterContextMenuInfo)menuInfo;
 58         //2-得到数据在集合中的索引
 59         index = acmi.position;
 60     }
 61     //响应菜单点击的回调方法
 62     @Override
 63     public boolean onContextItemSelected(MenuItem item) {
 64         switch (item.getItemId())
 65         {
 66             case 1:
 67                 //修改
 68                 final EditText editText = new EditText(this);
 69                 editText.setHint("输入电话号码");
 70                 editText.setInputType(InputType.TYPE_CLASS_PHONE);
 71                 editText.setText(alb.get(index).getPhoneNumber());
 72                 new AlertDialog.Builder(this)
 73                         .setTitle("修改黑名单")
 74                         .setView(editText)
 75                         .setPositiveButton("保存", new DialogInterface.OnClickListener() {
 76                             @Override
 77                             public void onClick(DialogInterface dialog, int which) {
 78                                 //1-得到新数据的实体类
 79                                 //赋值
 80                                 //1-传值,复制新的值再传递,值类型
 81                                 //2-传址,传递的是内存地址,指向同一个对象,引用类型
 82                                 BlackList blacklist = alb.get(index);
 83                                 blacklist.setPhoneNumber(editText.getText().toString());
 84                                 //2-调用DAO的update()
 85                                 if (blackListDAO.update(blacklist)>0)
 86                                 {
 87                                     Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
 88                                 }
 89                                 else
 90                                 {
 91                                     Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
 92                                 }
 93                             }
 94                         })
 95                         .setNeutralButton("取消", null)
 96                         .show();
 97                 break;
 98             case 2:
 99                 //添加删除确认对话框
100                 new AlertDialog.Builder(this)
101                         .setTitle("确认对话框")
102                         .setMessage("您确定删除该数据吗?")
103                         .setPositiveButton("确认", new DialogInterface.OnClickListener() {
104                             @Override
105                             public void onClick(DialogInterface dialog, int which) {
106                                 if (blackListDAO.delete(alb.get(index).getId())>0)
107                                 {
108                                     Toast.makeText(MainActivity.this, "删除成功!", Toast.LENGTH_SHORT).show();
109                                     alb.remove(index);
110                                 }
111                                 else
112                                 {
113                                     Toast.makeText(MainActivity.this, "删除失败!", Toast.LENGTH_SHORT).show();
114                                 }
115                             }
116                         })
117                         .setNegativeButton("取消", null)
118                         .show();
119         }
120         return super.onContextItemSelected(item);
121     }
122 
123     //baseadapter 实现类
124     class BLAdapter extends BaseAdapter
125     {
126         @Override
127         public int getCount() {
128             return alb.size();
129         }
130 
131         @Override
132         public Object getItem(int position) {
133             return alb.get(position);
134         }
135 
136         @Override
137         public long getItemId(int position) {
138             return alb.get(position).getId();
139         }
140 
141         @Override
142         public View getView(int position, View convertView, ViewGroup parent) {
143             //得到数据
144             BlackList blackList = alb.get(position);
145             //得到视图
146             if (convertView==null)
147             {
148                 //构建视图
149                 convertView = new TextView(MainActivity.this);
150             }
151             //视图和数据做显示匹配
152             TextView textView = (TextView)convertView;
153             textView.setTextSize(20);
154             textView.setHeight(100);
155             textView.setText(blackList.getPhoneNumber());
156             return textView;
157         }
158     }
159     //添加按钮监听器
160     public void add_onclick(View v)
161     {
162         //自定义对话框
163         final EditText editText = new EditText(this);
164         editText.setHint("输入电话号码");
165         editText.setInputType(InputType.TYPE_CLASS_NUMBER);
166 
167         //构建对话框
168         new AlertDialog.Builder(this)
169                 .setTitle("添加黑名单")
170                 .setView(editText)
171                 .setCancelable(false)
172                 .setNegativeButton("取消", null)
173                 .setPositiveButton("保存", new DialogInterface.OnClickListener() {
174                     @Override
175                     public void onClick(DialogInterface dialog, int which) {
176                         //向数据库保存 1-定义实体类
177                         BlackList blacklist = new BlackList(editText.getText().toString());
178                         //2-插入DAO数据
179                         long l = blackListDAO.insert(blacklist);
180                         if (l>0)
181                         {
182                             Toast.makeText(MainActivity.this, "电话号码保存成功!", Toast.LENGTH_SHORT).show();
183                             //更新list
184                             blacklist.setId(l);
185                             alb.add(0,blacklist);
186                         }
187                         else
188                         {
189                             Toast.makeText(MainActivity.this, "电话号码保存失败!", Toast.LENGTH_SHORT).show();
190                         }
191                     }
192                 })
193                 .show();
194     }
195 }

6.自定义对话框代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:orientation="vertical"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7 
 8     <EditText
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:hint="输入手机号码"
12         android:id="@+id/b1"
13         android:layout_margin="16dp"/>
14 </LinearLayout>

 

 

 

以上是关于黑名单管理代码总结的主要内容,如果未能解决你的问题,请参考以下文章

两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。 已抽签决定比赛名单。有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。(代码片段

VS中添加自定义代码片段——偷懒小技巧

python常用代码片段总结

BootStrap有用代码片段(持续总结)

BootStrap实用代码片段(持续总结)

使用 Git 来管理 Xcode 中的代码片段