Android——黑名单
Posted 柒寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android——黑名单相关的知识,希望对你有一定的参考价值。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.blacklist.MainActivity" 11 android:orientation="vertical"> 12 13 <ListView 14 android:layout_width="match_parent" 15 android:layout_height="0dp" 16 android:layout_weight="1" 17 android:id="@+id/lv_1"> 18 </ListView> 19 <Button 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="添加黑名单" 23 android:onClick="add_onClick"/> 24 </LinearLayout>
1 package com.hanqi.blacklist.com.hanqi.blacklist.orm; 2 3 /** 4 * Created by lenovo on 2016/6/6. 5 */ 6 //实体类 7 public class BlacList { 8 9 private long id; 10 private String phoneNumber; 11 12 public long getId() { 13 return id; 14 } 15 16 public void setId(long id) { 17 this.id = id; 18 } 19 20 public String getPhoneNumber() { 21 return phoneNumber; 22 } 23 24 public void setPhoneNumber(String phoneNumber) { 25 this.phoneNumber = phoneNumber; 26 } 27 28 public BlacList(long id, String phoneNumber) { 29 this.id = id; 30 this.phoneNumber = phoneNumber; 31 } 32 33 public BlacList(String phoneNumber) { 34 this.phoneNumber = phoneNumber; 35 } 36 }
1 package com.hanqi.blacklist.com.hanqi.blacklist.orm; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 8 import java.util.ArrayList; 9 10 /** 11 * Created by lenovo on 2016/6/6. 12 */ 13 //数据库操作类 14 public class BlackListDAO { 15 16 //private Context context; 17 private final String TABLE_NAME = "t_blacklist"; 18 private DBHelper dh; 19 20 public BlackListDAO(Context context) 21 { 22 //this.context = context; 23 dh = new DBHelper(context); 24 } 25 //增 26 //传入参数:实体类的实例 27 public long insert(BlacList blacList) 28 { 29 long rtn = 0; 30 //链接数据库 31 SQLiteDatabase sd = dh.getWritableDatabase(); 32 //执行insert语句 33 //insert into t_blacklist (phone_number) values () 34 ContentValues cv = new ContentValues(); 35 cv.put("phone_number",blacList.getPhoneNumber()); 36 rtn = sd.insert("t_blacklist",null,cv); 37 sd.close(); 38 return rtn; 39 } 40 //删 41 public int delete(long id) 42 { 43 int rtn = 0; 44 //链接数据库 45 SQLiteDatabase sd = dh.getWritableDatabase(); 46 //delete from t_blacklist where _id = ? 47 rtn = sd.delete(TABLE_NAME,"_id = ?",new String[]{id+""}); 48 sd.close(); 49 return rtn; 50 } 51 //改 52 public int update(BlacList blacList) 53 { 54 int rtn = 0; 55 //链接数据库 56 SQLiteDatabase sd = dh.getWritableDatabase(); 57 //update t_blacklist set phone_number = ? where _id = ? 58 ContentValues cv = new ContentValues(); 59 cv.put("phone_number",blacList.getPhoneNumber()); 60 rtn = sd.update(TABLE_NAME,cv,"_id =?",new String[]{blacList.getId()+""}); 61 sd.close(); 62 return rtn; 63 } 64 //查 65 //返回查询结果 66 public ArrayList<BlacList> getAll() 67 { 68 ArrayList<BlacList> blacLists = new ArrayList<>(); 69 //链接数据库 70 SQLiteDatabase sd = dh.getWritableDatabase(); 71 //select * from t_blacklist 72 //查询之后得到游标结果集 73 Cursor cursor = sd.query(TABLE_NAME, null, null, null, null, null, "_id desc"); 74 //遍历结果集 75 while (cursor.moveToNext()) 76 { 77 //1.把数据转成实体类的实例 78 BlacList blacList = new BlacList(cursor.getLong(0),cursor.getString(1)); 79 //2.把实例放在集合里,返回这个集合 80 blacLists.add(blacList); 81 } 82 cursor.close(); 83 sd.close(); 84 //返回这个集合 85 return blacLists; 86 } 87 }
1 package com.hanqi.blacklist.com.hanqi.blacklist.orm; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 import android.util.Log; 7 8 /** 9 * Created by lenovo on 2016/6/7. 10 */ 11 public class DBHelper extends SQLiteOpenHelper { 12 13 public DBHelper(Context context) { 14 super(context, "blacklist.db", null, 1); 15 } 16 17 @Override 18 public void onCreate(SQLiteDatabase db) { 19 20 //1.执行创建数据库的语句 21 String sql = "CREATE TABLE t_blacklist " + 22 "(_id INTEGER NOT NULL," + 23 "phone_number VARCHAR(20) NOT NULL," 24 +"PRIMARY KEY (\\"_id\\"))"; 25 db.execSQL(sql); 26 Log.e("TAG", "表创建成功"); 27 } 28 29 @Override 30 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 31 32 } 33 }
1 package com.hanqi.blacklist; 2 3 import android.app.AlertDialog; 4 import android.content.DialogInterface; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.text.InputType; 8 import android.view.ContextMenu; 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.TextView; 17 import android.widget.Toast; 18 19 import com.hanqi.blacklist.com.hanqi.blacklist.orm.BlacList; 20 import com.hanqi.blacklist.com.hanqi.blacklist.orm.BlackListDAO; 21 22 import java.util.ArrayList; 23 24 public class MainActivity extends AppCompatActivity { 25 26 ListView lv_1; 27 //数据访问对象 28 BlackListDAO bld = new BlackListDAO(this); 29 //数据集合 30 ArrayList<BlacList> alb; 31 32 BLAdapter bla; 33 34 //长按数据的索引 35 int index; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_main); 41 lv_1 = (ListView)findViewById(R.id.lv_1); 42 //增加上下文菜单,设置创建上下文菜单的监听器 43 lv_1.setOnCreateContextMenuListener(this); 44 //获取数据集合 45 alb = bld.getAll(); 46 //显示数据 47 //adapter 48 bla = new BLAdapter(); 49 lv_1.setAdapter(bla); 50 } 51 //重写创建上下文菜单的方法 52 @Override 53 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 54 super.onCreateContextMenu(menu, v, menuInfo); 55 menu.add(0, 1, 1, "修改"); 56 menu.add(0, 2, 2, "删除"); 57 //获取长按的数据信息 58 //1.得到菜单信息 59 AdapterView.AdapterContextMenuInfo acmi = 60 (AdapterView.AdapterContextMenuInfo)menuInfo; 61 //2.得到数据在集合中的索引 62 index = acmi.position; 63 } 64 //响应菜单点击的回调方法 65 @Override 66 public boolean onContextItemSelected(MenuItem item) { 67 68 switch (item.getItemId()) 69 { 70 case 1: 71 //修改 72 final EditText editText = new EditText(this); 73 editText.setHint("输入电话号码"); 74 editText.setInputType(InputType.TYPE_CLASS_PHONE); 75 editText.setText(alb.get(index).getPhoneNumber()); 76 new AlertDialog.Builder(this) 77 .setTitle("修改") 78 .setView(editText) 79 .setCancelable(false) 80 .setNegativeButton("取消", null) 81 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 82 @Override 83 public void onClick(DialogInterface dialog, int which) { 84 // BlacList blacList = new BlacList(alb.get(index).getId(),editText.getText().toString()); 85 // bld.update(blacList); 86 // alb = bld.getAll(); 87 // bla.notifyDataSetChanged(); 88 //1.得到新数据的实体类 89 //赋值: 90 // 1)传值,复制新的值再传递,值类型; 91 // 2)传址,传递的是内存的地址,指向同一个对象,引用类型 92 BlacList blaclist = alb.get(index); 93 blaclist.setPhoneNumber(editText.getText().toString()); 94 //2.调用DAO的update() 95 if (bld.update(blaclist)>0) 96 { 97 Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); 98 99 bla.notifyDataSetChanged(); 100 } 101 else 102 { 103 Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show(); 104 } 105 } 106 }) 107 .show(); 108 break; 109 case 2: 110 //添加删除确认对话框 111 //删除 112 new AlertDialog.Builder(this) 113 .setTitle("确认对话框") 114 .setMessage("确认删除么?") 115 .setNegativeButton("取消", null) 116 .setPositiveButton("确认", new DialogInterface.OnClickListener() { 117 @Override 118 public void onClick(DialogInterface dialog, int which) { 119 bld.delete(alb.get(index).getId()); 120 Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); 121 //alb = bld.getAll(); 122 alb.remove(index); 123 bla.notifyDataSetChanged(); 124 } 125 }) 126 .show(); 127 128 break; 129 } 130 return super.onContextItemSelected(item); 131 } 132 133 //BaseAdapter的实现类 134 class BLAdapter extends BaseAdapter 135 { 136 @Override 137 public int getCount() { 138 return alb.size(); 139 } 140 141 @Override 142 public Object getItem(int position) { 143 return alb.get(position); 144 } 145 146 @Override 147 public long getItemId(int position) { 148 return alb.get(position).getId(); 149 } 150 151 @Override 152 public View getView(int position, View convertView, ViewGroup parent) { 153 //得到数据 154 BlacList blacList = alb.get(position); 155 //得到视图 156 if (convertView == null) 157 { 158 //构建视图 159 convertView = new TextView(MainActivity.this); 160 } 161 //视图和数据做显示匹配 162 TextView textView = (TextView)convertView; 163 textView.setTextSize(20); 164 textView.setHeight(80); 165 textView.setText(blacList.getPhoneNumber()); 166 return textView; 167 } 168 } 169 //添加 170 public void add_onClick(View v) 171 { 172 //自定义对话框 173 final EditText editText = new EditText(this); 174 editText.setHint("输入电话号码"); 175 editText.setInputType(InputType.TYPE_CLASS_PHONE); 176 //构建对话框 177 new AlertDialog.Builder(this) 178 .setTitle("添加黑名单") 179 .setView(editText) 180 .setCancelable(false) 181 .setNegativeButton("取消", null) 182 .setPositiveButton("保存", new DialogInterface.OnClickListener() { 183 @Override 184 public void onClick(DialogInterface dialog, int which) { 185 //向数据库保存 186 //1.定义实体类 187 BlacList blacList = new BlacList(editText.getText().toString()); 188 //2.通过DAO插入数据 189 long l = bld.insert(blacList); 190 if (l > 0) { 191 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); 192 //更新list 193 //alb = bld.getAll(); 194 blacList.setId(l); 195 alb.add(0,blacList); 196 //刷新列表 197 bla.notifyDataSetChanged(); 198 } 199 else { 200 Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); 201 } 202 } 203 }) 204 .show(); 205 } 206 }
以上是关于Android——黑名单的主要内容,如果未能解决你的问题,请参考以下文章