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 Administrator 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 
21         //1.执行创建数据库的语句
22         String sql="CREATE TABLE t_blacklist" +
23                 "(_id  INTEGER PRIMARY KEY " +
24                 "AUTOINCREMENT NOT NULL," +
25                 "phone_number  VARCHAR(20)  NULL)";
26 
27         db.execSQL(sql);
28 
29         Log.e("TAG", "表创建成功");
30 
31     }
32 
33     @Override
34     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
35 
36     }
37 }
DBHelper
  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 import android.util.Log;
  8 
  9 import java.util.ArrayList;
 10 
 11 /**
 12  * Created by Administrator on 2016/6/6.
 13  */
 14 
 15 //数据库操作类
 16 public class BlackListDAO {
 17 
 18     //private Context context;
 19 
 20     private DBHelper dh;
 21 
 22     private final String TABLE_NAME="t_blacklist";
 23 
 24     public BlackListDAO(Context context)
 25     {
 26         //this.context=context;
 27 
 28         dh=new DBHelper(context);
 29     }
 30 
 31     // 32     //传入参数:实体类的实例
 33     public long insert(BlackList blackList)
 34     {
 35         long rtn=0;
 36 
 37         //连接数据库
 38         SQLiteDatabase sd=dh.getWritableDatabase();
 39         //执行insert语句
 40         //insert  into  t_blacklist(phone_number)  values()
 41 
 42         ContentValues cv=new ContentValues();
 43 
 44         cv.put("phone_number",blackList.getPhoneNumber());
 45 
 46         rtn=sd.insert(TABLE_NAME,null,cv);
 47 
 48         sd.close();
 49 
 50         return rtn;
 51     }
 52 
 53     //
 54     public int delete(long id)
 55     {
 56         int rtn=0;
 57 
 58         //连接数据库
 59         SQLiteDatabase sd=dh.getWritableDatabase();
 60 
 61         //delete from t_blacklist where _id=?
 62 
 63         rtn=sd.delete(TABLE_NAME,"_id=?",new String[]{id+""});
 64 
 65         sd.close();
 66 
 67         return  rtn;
 68 
 69     }
 70 
 71     //
 72     public int update(BlackList blackList)
 73     {
 74         int rtn=0;
 75 
 76         //连接数据库
 77         SQLiteDatabase sd=dh.getWritableDatabase();
 78         //update t_blacklist  set phone_number=? where _id=?
 79 
 80         ContentValues cv=new ContentValues();
 81         cv.put("phone_number",blackList.getPhoneNumber());
 82 
 83         rtn=sd.update(TABLE_NAME, cv, "_id=?", new String[]{blackList.getId()+""});
 84 
 85         sd.close();
 86 
 87         return  rtn;
 88 
 89     }
 90 
 91     // 92     //返回查询结果
 93     public ArrayList<BlackList> getAll()
 94     {
 95 
 96         ArrayList<BlackList> blackLists=new ArrayList<>();
 97 
 98         //连接数据库
 99         SQLiteDatabase sd=dh.getWritableDatabase();
100 
101         //select *  from  t_blacklist
102 
103         //查询之后得到游标结果集
104         Cursor cursor=sd.query(TABLE_NAME, null, null, null, null, null, "_id desc");
105 
106         //遍历结果集
107         while (cursor.moveToNext())
108         {
109             //1.把数据转成实体类的实例
110 
111             BlackList blackList = new BlackList(cursor.getLong(0), cursor.getString(1));
112 
113             //2.把实例放在集合里
114 
115             blackLists.add(blackList);
116         }
117         cursor.close();
118 
119         sd.close();
120 
121         //返回这个集合
122         return blackLists;
123 
124 
125 
126 
127 
128     }
129 }
BlackListDAO
 1 package com.hanqi.blacklist.com.hanqi.blacklist.orm;
 2 
 3 /**
 4  * Created by Administrator on 2016/6/6.
 5  */
 6 public class BlackList {
 7 
 8     private  long id;
 9     private  String phoneNumber;
10 
11     public long getId() {
12         return id;
13     }
14 
15     public void setId(long id) {
16         this.id = id;
17     }
18 
19     public String getPhoneNumber() {
20         return phoneNumber;
21     }
22 
23     public void setPhoneNumber(String phoneNumber) {
24         this.phoneNumber = phoneNumber;
25     }
26 
27     public BlackList(long id, String phoneNumber) {
28         this.id = id;
29         this.phoneNumber = phoneNumber;
30     }
31 
32     public BlackList(String phoneNumber) {
33         this.phoneNumber = phoneNumber;
34     }
35 }
BlackList
 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="16dp"
 7     android:paddingLeft="16dp"
 8     android:paddingRight="16dp"
 9     android:paddingTop="16dp"
10     tools:context="com.hanqi.blacklist.MainActivity"
11     android:orientation="vertical">
12 
13 
14     <ListView
15         android:layout_width="match_parent"
16         android:layout_height="0dp"
17         android:layout_weight="1"
18         android:id="@+id/lv_1">
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 </LinearLayout>
main_xml
 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="16dp"
 7     android:paddingLeft="16dp"
 8     android:paddingRight="16dp"
 9     android:paddingTop="16dp"
10     tools:context="com.hanqi.blacklist.MainActivity"
11     android:orientation="vertical">
12 
13 
14     <ListView
15         android:layout_width="match_parent"
16         android:layout_height="0dp"
17         android:layout_weight="1"
18         android:id="@+id/lv_1">
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 </LinearLayout>
main_xml
  1 package com.hanqi.blacklist;
  2 
  3 import android.app.AlertDialog;
  4 import android.content.DialogInterface;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.os.Bundle;
  7 import android.text.InputType;
  8 import android.util.Log;
  9 import android.view.ContextMenu;
 10 import android.view.MenuItem;
 11 import android.view.View;
 12 import android.view.ViewGroup;
 13 import android.widget.Adapter;
 14 import android.widget.AdapterView;
 15 import android.widget.BaseAdapter;
 16 import android.widget.EditText;
 17 import android.widget.ListView;
 18 import android.widget.TextView;
 19 import android.widget.Toast;
 20 
 21 import com.hanqi.blacklist.com.hanqi.blacklist.orm.BlackList;
 22 import com.hanqi.blacklist.com.hanqi.blacklist.orm.BlackListDAO;
 23 
 24 import java.util.ArrayList;
 25 
 26 public class MainActivity extends AppCompatActivity {
 27 
 28     ListView lv_1;
 29 
 30     //数据访问对象
 31     BlackListDAO bld=new BlackListDAO(this);
 32 
 33     //数据集合
 34     ArrayList<BlackList> alb;
 35 
 36     BaseAdapter bla;
 37 
 38     //长按的数据的索引
 39     int index;
 40 
 41     @Override
 42     protected void onCreate(Bundle savedInstanceState) {
 43         super.onCreate(savedInstanceState);
 44         setContentView(R.layout.activity_main);
 45 
 46         lv_1=(ListView)findViewById(R.id.lv_1);
 47 
 48         //增加上下文菜单,设置创建菜单的监听器
 49 
 50         lv_1.setOnCreateContextMenuListener(this);
 51 
 52         //获取数据集合
 53         alb=bld.getAll();
 54 
 55         //显示数据
 56         //Adapter
 57         bla=new BLAdapter();
 58 
 59         lv_1.setAdapter(bla);
 60 
 61 
 62     }
 63 
 64     //重写创建上下文菜单的方法
 65     @Override
 66     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
 67         super.onCreateContextMenu(menu, v, menuInfo);
 68 
 69         menu.add(0, 1, 1, "修改");
 70         menu.add(1, 2, 2, "删除");
 71 
 72 
 73         //获取长按的数据信息
 74         //1.得到菜单信息
 75         AdapterView.AdapterContextMenuInfo acmi=
 76                 (AdapterView.AdapterContextMenuInfo)menuInfo;
 77 
 78         //2.得到数据在集合中的索引
 79         index=acmi.position;
 80     }
 81 
 82     //响应菜单点击的回调方法
 83     @Override
 84     public boolean onContextItemSelected(final MenuItem item) {
 85 
 86         switch (item.getItemId())
 87         {
 88             case 1:
 89                 //修改
 90                 final EditText editText=new EditText(this);
 91                 editText.setHint("请输入电话号码");
 92                 editText.setInputType(InputType.TYPE_CLASS_PHONE);
 93 
 94                 editText.setText(alb.get(index).getPhoneNumber());
 95 
 96                 new AlertDialog.Builder(this)
 97                         .setTitle("修改黑名单")
 98                         .setView(editText)
 99                         .setPositiveButton("保存", new DialogInterface.OnClickListener() {
100                             @Override
101                             public void onClick(DialogInterface dialog, int which) {
102 
103                                 //1.得到新数据的实体类
104                                 //赋值:
105                                 // 1)传值,赋值新的值在传递;
106                                 // 2)传址,传递的是内存地址,指向同一个对象,引用类型
107                                 BlackList blackList=alb.get(index);
108 
109                                 blackList.setPhoneNumber(editText.getText().toString());
110 
111                                 //2.调用DAO的update()
112                                 if (bld.update(blackList)>0)
113                                 {
114                                     Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
115 
116                                     //更新List
117 
118                                     //刷新列表
119                                     bla.notifyDataSetChanged();
120 
121                                 }else {
122 
123                                     Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
124                                 }
125 
126                                 //3.刷新ListView
127                             }
128                         })
129                         .setNegativeButton("取消", null)
130                         .setCancelable(false)
131                         .show();
132 
133                 break;
134             case 2:
135 
136                 //添加删除确认对话框
137                 new AlertDialog.Builder(this)
138                         .setTitle("删除")
139                         .setMessage("是否确定要删除?")
140                         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
141                             @Override
142                             public void onClick(DialogInterface dialog, int which) {
143 
144                                 //删除
145                                 if (bld.delete(alb.get(index).getId())>0) {
146 
147                                     Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
148 
149                                     //更新List
150                                     //alb = bld.getAll();
151                                     alb.remove(index);
152 
153                                     //刷新列表
154                                     bla.notifyDataSetChanged();
155                                 }
156                                 else {
157                                     Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
158                                 }
159                             }
160                         })
161                         .setNegativeButton("取消", null)
162                         .setCancelable(false)
163                         .show();
164 
165                 break;
166         }
167         return super.onContextItemSelected(item);
168     }
169 
170     //BaseAdapter的实现类
171     class BLAdapter extends BaseAdapter
172     {
173         @Override
174         public int getCount() {
175             return alb.size();
176         }
177 
178         @Override
179         public Object getItem(int position) {
180             return alb.get(position);
181         }
182 
183         @Override
184         public long getItemId(int position) {
185             return alb.get(position).getId();
186         }
187 
188         @Override
189         public View getView(int position, View convertView, ViewGroup parent) {
190 
191             //得到数据
192             BlackList blackList=alb.get(position);
193 
194             //得到视图
195             if(convertView==null)
196             {
197                 //构建视图
198                 convertView=new TextView(MainActivity.this);
199             }
200 
201             //视图和数据做显示匹配
202             TextView textView=(TextView)convertView;
203 
204             textView.setTextSize(20);
205 
206             textView.setHeight(80);
207 
208             textView.setText(blackList.getPhoneNumber());
209 
210             return textView;
211         }
212     }
213 
214     //添加
215     public void add_OnClick(View v)
216     {
217         //自定义对话框
218         final EditText editText=new EditText(this);
219         editText.setHint("输入电话号码");
220         editText.setInputType(InputType.TYPE_CLASS_PHONE);
221 
222 
223         //构建对话框
224         new AlertDialog.Builder(this)
225                 .setTitle("添加黑名单")
226                 .setView(editText)
227                 .setCancelable(false)
228                 .setNegativeButton("取消", null)
229                 .setPositiveButton("保存", new DialogInterface.OnClickListener() {
230                     @Override
231                     public void onClick(DialogInterface dialog, int which) {
232 
233                         //向数据库保存
234                         //1.定义实体类
235                         BlackList blackList = new BlackList(editText.getText().toString());
236 
237                         //2.通过DAO插入数据
238                         long l = bld.insert(blackList);
239 
240                         if (l > 0) {
241 
242                             Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
243 
244                             //更新List
245                             //alb=bld.getAll();
246                             blackList.setId(l);
247                             alb.add(0,blackList);
248 
249                             //刷新列表
250                             bla.notifyDataSetChanged();
251 
252                         }
253                         else {
254 
255                             Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
256                         }
257                     }
258                 })
259                 .show();
260     }
261 }
main_java

 

posted on 2016-06-08 15:58  安然罒  阅读(259)  评论(0编辑  收藏  举报