黑名单项目
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 19 </ListView> 20 21 <Button 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:text="添加黑名单" 25 android:onClick="add_OnClick"/> 26 27 28 </LinearLayout>
1 vvpackage 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.BlackList; 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 //数据访问对象 29 BlackListDAO bld = new BlackListDAO(this); 30 31 //数据集合 32 ArrayList<BlackList> alb; 33 34 BLAdapter bla; 35 36 //长按的数据的索引 37 int index; 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.activity_main); 43 44 lv_1 = (ListView)findViewById(R.id.lv_1); 45 46 //增加上下文菜单,设置创建菜单的监听器 47 48 lv_1.setOnCreateContextMenuListener(this); 49 50 //获取数据 51 alb = bld.getAll(); 52 53 //显示数据(listview 适配器 adapter) 54 //adapter 55 bla = new BLAdapter(); 56 57 lv_1.setAdapter(bla); 58 59 } 60 61 //重写创建上下文菜单的方法 62 @Override 63 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 64 super.onCreateContextMenu(menu, v, menuInfo); 65 66 menu.add(0, 1, 1, "修改"); 67 menu.add(0, 2, 2, "删除"); 68 69 //获取长按的数据信息 70 //1.得到菜单信息 71 AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo)menuInfo; 72 73 //2.得到数据在集合中的索引 74 index = acmi.position; 75 } 76 77 //响应菜单点击的回调方法 78 @Override 79 public boolean onContextItemSelected(MenuItem item) { 80 81 switch (item.getItemId()) 82 { 83 case 1: 84 //修改 85 final EditText editText = new EditText(this); 86 editText.setHint("输入电话号码"); 87 editText.setInputType(InputType.TYPE_CLASS_PHONE); 88 89 editText.setText(alb.get(index).getPhoneNumber()); 90 91 new AlertDialog.Builder(this) 92 .setTitle("修改黑名单") 93 .setView(editText) 94 .setCancelable(false) 95 .setNegativeButton("取消", null) 96 .setPositiveButton("保存", new DialogInterface.OnClickListener() { 97 @Override 98 public void onClick(DialogInterface dialog, int which) { 99 100 //1.得到新数据的实体类 101 //赋值: 102 // 1)传值;复制新的值再传递;值类型; 103 // 2)传址;传递的是内存地址,指向同一个对象引用类型 104 BlackList blacklist = alb.get(index); 105 106 blacklist.setPhoneNumber(editText.getText().toString()); 107 108 //2.调用DAO的update() 109 if (bld.update(blacklist)>0) 110 { 111 Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); 112 113 //更新List 114 115 //刷新列表 116 bla.notifyDataSetChanged(); 117 } 118 else 119 { 120 Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show(); 121 } 122 123 //3.刷新ListView 124 125 // BlackList blacklist = new BlackList(editText.getText().toString()); 126 // 127 // 128 // long k = bld.insert(blacklist); 129 // 130 // if (k > 0) 131 // { 132 // Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); 133 // 134 // 135 // blacklist.setId(k); 136 // alb.add(0, blacklist); 137 // 138 // 139 // bla.notifyDataSetChanged(); 140 // } 141 // 142 // 143 // else 144 // { 145 // Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); 146 // } 147 148 } 149 }) 150 .show(); 151 break; 152 case 2: 153 //添加删除确认对话框 154 new AlertDialog.Builder(this) 155 .setTitle("是否确定删除") 156 .setMessage("是否确定删除?") 157 .setCancelable(false) 158 .setNegativeButton("取消", null) 159 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 160 @Override 161 public void onClick(DialogInterface dialog, int which) { 162 163 //删除 164 if (bld.delete(alb.get(index).getId())>0) { 165 166 Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); 167 168 //更新list 169 //alb = bld.getAll(); 170 alb.remove(index); 171 172 //刷新列表 173 bla.notifyDataSetChanged(); 174 } 175 else 176 { 177 Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show(); 178 } 179 180 } 181 }) 182 183 .show(); 184 185 break; 186 } 187 188 return super.onContextItemSelected(item); 189 } 190 191 //BaseAdapter的实现类 192 class BLAdapter extends BaseAdapter 193 { 194 @Override 195 public int getCount() { 196 return alb.size(); 197 } 198 199 @Override 200 public Object getItem(int position) { 201 return alb.get(position); 202 } 203 204 @Override 205 public long getItemId(int position) { 206 return alb.get(position).getId(); 207 } 208 209 @Override 210 public View getView(int position, View convertView, ViewGroup parent) { 211 212 //得到数据 213 BlackList blackList = alb.get(position); 214 215 //得到视图 216 if (convertView == null) 217 { 218 //构建视图 219 convertView = new TextView(MainActivity.this); 220 } 221 222 //视图和数据做显示匹配 223 TextView textView = (TextView)convertView; 224 225 textView.setTextSize(20); 226 227 textView.setHeight(80); 228 229 textView.setText(blackList.getPhoneNumber()); 230 231 return textView; 232 } 233 } 234 235 //添加 236 public void add_OnClick(View v) 237 { 238 //自定义对话框 239 final EditText editText = new EditText(this); 240 editText.setHint("输入电话号码"); 241 editText.setInputType(InputType.TYPE_CLASS_PHONE); 242 243 //构建对话框 244 new AlertDialog.Builder(this) 245 .setTitle("添加黑名单") 246 .setView(editText) 247 .setCancelable(false) 248 .setNegativeButton("取消", null) 249 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 250 @Override 251 public void onClick(DialogInterface dialog, int which) { 252 253 //向数据库保存 254 //1.定义实体类 255 BlackList blackList = new BlackList(editText.getText().toString()); 256 257 //2.通过DAO插入数据 258 long l = bld.insert(blackList); 259 260 if (l > 0) 261 { 262 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); 263 264 //更新list 265 //alb = bld.getAll(); 266 blackList.setId(l); 267 alb.add(0, blackList); 268 269 //刷新列表 270 bla.notifyDataSetChanged(); 271 } 272 273 274 else 275 { 276 Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); 277 } 278 } 279 }) 280 281 .show(); 282 } 283 }