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.contacts.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 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:layout_width="0dp" 9 android:layout_height="wrap_content" 10 android:layout_weight="1" 11 android:id="@+id/tv_1" 12 android:textSize="30dp" 13 android:height="40dp"/> 14 <TextView 15 android:layout_width="0dp" 16 android:layout_height="wrap_content" 17 android:layout_weight="1" 18 android:id="@+id/tv_2" 19 android:textSize="30dp" 20 android:height="40dp"/> 21 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <EditText 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:hint="请输入姓名" 11 android:id="@+id/et_1"/> 12 <EditText 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:hint="请输入电话号码" 16 android:inputType="number" 17 android:id="@+id/et_2"/> 18 </LinearLayout>
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 }
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 @Override 17 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 @Override 29 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 30 31 } 32 }
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<Contacts> getAll() 59 { 60 //select * from t_contacts 61 //查询之后得到游标结果集 62 ArrayList<Contacts> 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 }
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<Contacts> 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 BaseAdapter 114 { 115 @Override 116 public int getCount() { 117 return act.size(); 118 } 119 120 @Override 121 public Object getItem(int position) { 122 return act.get(position); 123 } 124 125 @Override 126 public long getItemId(int position) { 127 return act.get(position).getId(); 128 } 129 130 @Override 131 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 @Override 157 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 以上是关于Android——通讯录的主要内容,如果未能解决你的问题,请参考以下文章