博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android——通讯录
阅读量:5330 次
发布时间:2019-06-14

本文共 11891 字,大约阅读时间需要 39 分钟。

1 
2
12 13
18 19
24
activity_main
1 
2
6 7
14
21
contact1_layout
1 
2
6 7
12
18
contact_layout
1 package com.hanqi.contacts.com.hanqi.contacts.orm; 2  3 /** 4  * Created by lenovo on 2016/6/8. 5  */ 6 public class Contacts { 7  8     private long id; 9     private String name;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 getName() {21         return name;22     }23 24     public void setName(String name) {25         this.name = name;26     }27 28     public String getPhoneNumber() {29         return phoneNumber;30     }31 32     public void setPhoneNumber(String phoneNumber) {33         this.phoneNumber = phoneNumber;34     }35 36     public Contacts(long id, String name, String phoneNumber) {37         this.id = id;38         this.name = name;39         this.phoneNumber = phoneNumber;40     }41 42     public Contacts(String name,String phoneNumber) {43         this.name = name;44         this.phoneNumber = phoneNumber;45     }46 }
Contacts
1 package com.hanqi.contacts.com.hanqi.contacts.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/8.10  */11 public class CTHelper extends SQLiteOpenHelper {12     public CTHelper(Context context) {13         super(context, "contacts.db", null, 1);14     }15 16     @Override17     public void onCreate(SQLiteDatabase db) {18         //1.执行创建数据库的语句19         String sql = "CREATE TABLE t_contacts " +20                 "(_id  INTEGER NOT NULL," +21                 "name  VARCHAR(20) NOT NULL,"22                 +"phone_number  VARCHAR(20) NOT NULL,"23                 +"PRIMARY KEY (\"_id\"))";24         db.execSQL(sql);25         Log.e("TAG", "表创建成功");26     }27 28     @Override29     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {30 31     }32 }
CTHelper
1 package com.hanqi.contacts.com.hanqi.contacts.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/8.12  */13 public class ContactsDAO {14     private final String TABLE_NAME = "t_contacts";15     private CTHelper ch;16 17     public ContactsDAO(Context context)18     {19         ch = new CTHelper(context);20     }21     //增22     public long insert(Contacts contacts)23     {24         long rtn = 0;25         SQLiteDatabase sd = ch.getWritableDatabase();26         //insert into t_contacts (phoneNumber) values()27         ContentValues cv = new ContentValues();28         cv.put("name",contacts.getName());29         cv.put("phone_number",contacts.getPhoneNumber());30         rtn = sd.insert("t_contacts",null,cv);31         sd.close();32         return rtn;33     }34     //删35     public int delete(long id)36     {37         int rtn = 0;38         //delete from t_contacts where = ?39         SQLiteDatabase sd = ch.getWritableDatabase();40         rtn = sd.delete(TABLE_NAME,"_id = ?",new String[]{id+""});41         sd.close();42         return rtn;43     }44     //改45     public int update(Contacts contacts)46     {47         int rtn = 0;48         //update t_contacts set phone_number = ? and name = ? where id = ?49         SQLiteDatabase sd = ch.getWritableDatabase();50         ContentValues cv = new ContentValues();51         cv.put("name",contacts.getName());52         cv.put("phone_number",contacts.getPhoneNumber());53         rtn = sd.update(TABLE_NAME,cv,"_id = ?",new String[]{contacts.getId()+""});54         sd.close();55         return rtn;56     }57     //查58     public ArrayList
getAll()59 {60 //select * from t_contacts61 //查询之后得到游标结果集62 ArrayList
contacts = new ArrayList<>();63 SQLiteDatabase sd = ch.getWritableDatabase();64 Cursor cursor = sd.query(TABLE_NAME, null, null, null, null, null, "_id desc");65 while (cursor.moveToNext())66 {67 Contacts contacts1 = new Contacts(cursor.getLong(0),cursor.getString(1),cursor.getString(2));68 contacts.add(contacts1);69 }70 cursor.close();71 sd.close();72 return contacts;73 }74 }
ContactsDAO
1 package com.hanqi.contacts;  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.view.ContextMenu;  8 import android.view.MenuItem;  9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.AdapterView; 12 import android.widget.BaseAdapter; 13 import android.widget.EditText; 14 import android.widget.ListView; 15 import android.widget.TextView; 16 import android.widget.Toast; 17  18 import com.hanqi.contacts.com.hanqi.contacts.orm.Contacts; 19 import com.hanqi.contacts.com.hanqi.contacts.orm.ContactsDAO; 20  21 import java.util.ArrayList; 22  23 public class MainActivity extends AppCompatActivity { 24     ListView lv_1; 25     ContactsDAO ctd = new ContactsDAO(this); 26     ArrayList
act; 27 CTAdapter cta; 28 //长按数据的索引 29 int index; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 36 lv_1 = (ListView)findViewById(R.id.lv_1); 37 38 lv_1.setOnCreateContextMenuListener(this); 39 //获取数据集合 40 act = ctd.getAll(); 41 //显示数据 42 cta = new CTAdapter(); 43 lv_1.setAdapter(cta); 44 45 } 46 //重写创建上下文菜单的方法 47 @Override 48 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 49 super.onCreateContextMenu(menu, v, menuInfo); 50 menu.add(0, 1, 1, "修改"); 51 menu.add(0, 2, 2, "删除"); 52 //获取长按的数据信息 53 //1.得到菜单信息 54 AdapterView.AdapterContextMenuInfo acmi = 55 (AdapterView.AdapterContextMenuInfo)menuInfo; 56 //2.得到数据在集合中的索引 57 index = acmi.position; 58 } 59 //响应菜单点击的回调方法 60 @Override 61 public boolean onContextItemSelected(MenuItem item) { 62 switch (item.getItemId()) 63 { 64 case 1: 65 final View view = View.inflate(this,R.layout.contact_layout,null); 66 new AlertDialog.Builder(this) 67 .setTitle("添加通讯录") 68 .setView(view) 69 .setCancelable(false) 70 .setNegativeButton("取消", null) 71 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 72 @Override 73 public void onClick(DialogInterface dialog, int which) { 74 EditText et_1 = (EditText)view.findViewById(R.id.et_1); 75 EditText et_2 = (EditText)view.findViewById(R.id.et_2); 76 Contacts contacts = act.get(index); 77 contacts.setName(et_1.getText().toString()); 78 contacts.setPhoneNumber(et_2.getText().toString()); 79 if(ctd.update(contacts)>0) 80 { 81 Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); 82 cta.notifyDataSetChanged(); 83 } 84 else 85 { 86 Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show(); 87 } 88 } 89 }) 90 .show(); 91 break; 92 case 2: 93 new AlertDialog.Builder(this) 94 .setTitle("确认对话框") 95 .setCancelable(false) 96 .setNegativeButton("取消",null) 97 .setPositiveButton("确认", new DialogInterface.OnClickListener() { 98 @Override 99 public void onClick(DialogInterface dialog, int which) {100 ctd.delete(act.get(index).getId());101 Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();102 act = ctd.getAll();103 cta.notifyDataSetChanged();104 }105 })106 .show();107 break;108 }109 return super.onContextItemSelected(item);110 }111 112 //BaseAdapter的实现类113 class CTAdapter extends BaseAdapter114 {115 @Override116 public int getCount() {117 return act.size();118 }119 120 @Override121 public Object getItem(int position) {122 return act.get(position);123 }124 125 @Override126 public long getItemId(int position) {127 return act.get(position).getId();128 }129 130 @Override131 public View getView(int position, View convertView, ViewGroup parent) {132 //得到数据133 Contacts contacts = act.get(position);134 //得到视图135 if (convertView == null)136 {137 convertView = View.inflate(MainActivity.this,R.layout.contact1_layout,null);138 }139 //视图和数据做显式匹配140 TextView textView = (TextView)convertView.findViewById(R.id.tv_1);141 textView.setText(contacts.getName());142 TextView textView1 = (TextView)convertView.findViewById(R.id.tv_2);143 textView1.setText(contacts.getPhoneNumber());144 return convertView;145 }146 }147 public void add_onClick(View v)148 {149 final View view = View.inflate(this,R.layout.contact_layout,null);150 new AlertDialog.Builder(this)151 .setTitle("添加通讯录")152 .setView(view)153 .setCancelable(false)154 .setNegativeButton("取消", null)155 .setPositiveButton("保存", new DialogInterface.OnClickListener() {156 @Override157 public void onClick(DialogInterface dialog, int which) {158 EditText et_1 = (EditText)view.findViewById(R.id.et_1);159 EditText et_2 = (EditText)view.findViewById(R.id.et_2);160 Contacts contacts = new Contacts(et_1.getText().toString(), et_2.getText().toString());161 long l = ctd.insert(contacts);162 if (l > 0) {163 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();164 act = ctd.getAll();165 cta.notifyDataSetChanged();166 } else {167 Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();168 }169 }170 })171 .show();172 }173 }
MainActivity

转载于:https://www.cnblogs.com/hanazawalove/p/5575494.html

你可能感兴趣的文章
Android 监听返回键、HOME键
查看>>
Android ContentProvider的实现
查看>>
sqlserver 各种判断是否存在(表名、函数、存储过程等)
查看>>
给C#学习者的建议 - CLR Via C# 读后感
查看>>
Recover Binary Search Tree
查看>>
Java 实践:生产者与消费者
查看>>
[转]IOCP--Socket IO模型终结篇
查看>>
各种正则验证
查看>>
观察者模式(Observer)
查看>>
python中numpy.r_和numpy.c_
查看>>
egret3D与2D混合开发,画布尺寸不一致的问题
查看>>
freebsd 实现 tab 命令 补全 命令 提示
查看>>
struts1和struts2的区别
查看>>
函数之匿名函数
查看>>
shell习题第16题:查用户
查看>>
Redis常用命令
查看>>
2018.11.06 bzoj1040: [ZJOI2008]骑士(树形dp)
查看>>
2019.02.15 bzoj5210: 最大连通子块和(链分治+ddp)
查看>>
redis cluster 集群资料
查看>>
微软职位内部推荐-Sr. SE - Office incubation
查看>>