<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.contacts.MainActivity"
android:orientation="vertical"> <ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/lv_1">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加通讯录名单"
android:onClick="add_onClick"/>
</LinearLayout>

activity_main

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tv_1"
android:textSize="30dp"
android:height="40dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tv_2"
android:textSize="30dp"
android:height="40dp"/>
</LinearLayout>

contact1_layout

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:id="@+id/et_1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码"
android:inputType="number"
android:id="@+id/et_2"/>
</LinearLayout>

contact_layout

 package com.hanqi.contacts.com.hanqi.contacts.orm;

 /**
* Created by lenovo on 2016/6/8.
*/
public class Contacts { private long id;
private String name;
private String phoneNumber; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPhoneNumber() {
return phoneNumber;
} public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
} public Contacts(long id, String name, String phoneNumber) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
} public Contacts(String name,String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
}

Contacts

 package com.hanqi.contacts.com.hanqi.contacts.orm;

 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; /**
* Created by lenovo on 2016/6/8.
*/
public class CTHelper extends SQLiteOpenHelper {
public CTHelper(Context context) {
super(context, "contacts.db", null, 1);
} @Override
public void onCreate(SQLiteDatabase db) {
//1.执行创建数据库的语句
String sql = "CREATE TABLE t_contacts " +
"(_id INTEGER NOT NULL," +
"name VARCHAR(20) NOT NULL,"
+"phone_number VARCHAR(20) NOT NULL,"
+"PRIMARY KEY (\"_id\"))";
db.execSQL(sql);
Log.e("TAG", "表创建成功");
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}

CTHelper

 package com.hanqi.contacts.com.hanqi.contacts.orm;

 import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; /**
* Created by lenovo on 2016/6/8.
*/
public class ContactsDAO {
private final String TABLE_NAME = "t_contacts";
private CTHelper ch; public ContactsDAO(Context context)
{
ch = new CTHelper(context);
}
//增
public long insert(Contacts contacts)
{
long rtn = 0;
SQLiteDatabase sd = ch.getWritableDatabase();
//insert into t_contacts (phoneNumber) values()
ContentValues cv = new ContentValues();
cv.put("name",contacts.getName());
cv.put("phone_number",contacts.getPhoneNumber());
rtn = sd.insert("t_contacts",null,cv);
sd.close();
return rtn;
}
//删
public int delete(long id)
{
int rtn = 0;
//delete from t_contacts where = ?
SQLiteDatabase sd = ch.getWritableDatabase();
rtn = sd.delete(TABLE_NAME,"_id = ?",new String[]{id+""});
sd.close();
return rtn;
}
//改
public int update(Contacts contacts)
{
int rtn = 0;
//update t_contacts set phone_number = ? and name = ? where id = ?
SQLiteDatabase sd = ch.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name",contacts.getName());
cv.put("phone_number",contacts.getPhoneNumber());
rtn = sd.update(TABLE_NAME,cv,"_id = ?",new String[]{contacts.getId()+""});
sd.close();
return rtn;
}
//查
public ArrayList<Contacts> getAll()
{
//select * from t_contacts
//查询之后得到游标结果集
ArrayList<Contacts> contacts = new ArrayList<>();
SQLiteDatabase sd = ch.getWritableDatabase();
Cursor cursor = sd.query(TABLE_NAME, null, null, null, null, null, "_id desc");
while (cursor.moveToNext())
{
Contacts contacts1 = new Contacts(cursor.getLong(0),cursor.getString(1),cursor.getString(2));
contacts.add(contacts1);
}
cursor.close();
sd.close();
return contacts;
}
}

ContactsDAO

 package com.hanqi.contacts;

 import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.hanqi.contacts.com.hanqi.contacts.orm.Contacts;
import com.hanqi.contacts.com.hanqi.contacts.orm.ContactsDAO; import java.util.ArrayList; public class MainActivity extends AppCompatActivity {
ListView lv_1;
ContactsDAO ctd = new ContactsDAO(this);
ArrayList<Contacts> act;
CTAdapter cta;
//长按数据的索引
int index; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); lv_1 = (ListView)findViewById(R.id.lv_1); lv_1.setOnCreateContextMenuListener(this);
//获取数据集合
act = ctd.getAll();
//显示数据
cta = new CTAdapter();
lv_1.setAdapter(cta); }
//重写创建上下文菜单的方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 1, 1, "修改");
menu.add(0, 2, 2, "删除");
//获取长按的数据信息
//1.得到菜单信息
AdapterView.AdapterContextMenuInfo acmi =
(AdapterView.AdapterContextMenuInfo)menuInfo;
//2.得到数据在集合中的索引
index = acmi.position;
}
//响应菜单点击的回调方法
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId())
{
case 1:
final View view = View.inflate(this,R.layout.contact_layout,null);
new AlertDialog.Builder(this)
.setTitle("添加通讯录")
.setView(view)
.setCancelable(false)
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et_1 = (EditText)view.findViewById(R.id.et_1);
EditText et_2 = (EditText)view.findViewById(R.id.et_2);
Contacts contacts = act.get(index);
contacts.setName(et_1.getText().toString());
contacts.setPhoneNumber(et_2.getText().toString());
if(ctd.update(contacts)>0)
{
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
cta.notifyDataSetChanged();
}
else
{
Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
}
}
})
.show();
break;
case 2:
new AlertDialog.Builder(this)
.setTitle("确认对话框")
.setCancelable(false)
.setNegativeButton("取消",null)
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ctd.delete(act.get(index).getId());
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
act = ctd.getAll();
cta.notifyDataSetChanged();
}
})
.show();
break;
}
return super.onContextItemSelected(item);
} //BaseAdapter的实现类
class CTAdapter extends BaseAdapter
{
@Override
public int getCount() {
return act.size();
} @Override
public Object getItem(int position) {
return act.get(position);
} @Override
public long getItemId(int position) {
return act.get(position).getId();
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
//得到数据
Contacts contacts = act.get(position);
//得到视图
if (convertView == null)
{
convertView = View.inflate(MainActivity.this,R.layout.contact1_layout,null);
}
//视图和数据做显式匹配
TextView textView = (TextView)convertView.findViewById(R.id.tv_1);
textView.setText(contacts.getName());
TextView textView1 = (TextView)convertView.findViewById(R.id.tv_2);
textView1.setText(contacts.getPhoneNumber());
return convertView;
}
}
public void add_onClick(View v)
{
final View view = View.inflate(this,R.layout.contact_layout,null);
new AlertDialog.Builder(this)
.setTitle("添加通讯录")
.setView(view)
.setCancelable(false)
.setNegativeButton("取消", null)
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et_1 = (EditText)view.findViewById(R.id.et_1);
EditText et_2 = (EditText)view.findViewById(R.id.et_2);
Contacts contacts = new Contacts(et_1.getText().toString(), et_2.getText().toString());
long l = ctd.insert(contacts);
if (l > 0) {
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
act = ctd.getAll();
cta.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
})
.show();
}
}

MainActivity

Android——通讯录的更多相关文章

  1. Jquery Mobile设计Android通讯录第二章

    本文是jQuery Mobile设计Android通讯录系统教程的第二篇,在上一篇教程中(http://publish.itpub.net/a2011/0517/1191/000001191561.s ...

  2. android 通讯录实现

    最近项目需要,于是自己实现了一个带导航栏的通讯录,上代码! 一.数据准备 (1)bean: public class Friend { private String remark; private S ...

  3. 使用Jquery Mobile设计Android通讯录

    本系列教程将指导大家一步步使用Jquery Mobile设计一个Android的通讯录应用.其中在应用的界面部分,将使用jQuery Mobile框架,并且会指导大家如何使Android中提供的web ...

  4. android通讯录导航栏源码(一)

    通讯录导航栏源码: 1.activity package com.anna.contact.activity; import java.util.ArrayList; import java.util ...

  5. 【实用篇】获取Android通讯录中联系人信息

    第一步,在Main.xml布局文件中声明一个Button控件,布局文件代码如下: <LinearLayout xmlns:android="http://schemas.android ...

  6. Android Contacts (android通讯录读取)-content provider

    Content Provider 在数据处理中,Android通常使用Content Provider的方式.Content Provider使用Uri实例作为句柄的数据封装的,很方便地访问地进行数据 ...

  7. 实例源码--Android通讯录源码

      下载源码   技术要点: 1.通讯录联 系人的管理 2.接听.打电话 3.发短信 4. 源码带详细的 中文注释    ...... 详细介绍: 1.通讯录联系人的管理 播放器具有播放本地音乐的功能 ...

  8. Android通讯录管理(获取联系人、通话记录、短信消息)

    前言:前阵子主要是记录了如何对联系人的一些操作,比如搜索,全选.反选和删除等在实际开发中可能需要实现的功能,本篇博客是小巫从一个别人开源的一个项目抽取出来的部分内容,把它给简化出来,可以让需要的朋友清 ...

  9. android UI进阶之弹窗的使用(2)--实现通讯录的弹窗效果

    相信大家都体验过android通讯录中的弹窗效果.如图所示: android中提供了QuickContactBadge来实现这一效果.这里简单演示下. 首先创建布局文件: <?xml versi ...

随机推荐

  1. 在Win7 64位操作系统下安装Oracle 10g

    参见网址http://www.cnblogs.com/newstar/archive/2010/12/01/1878026.html 1.下载安装程序,可以到这个网址去下载 http://www.or ...

  2. Onedrive 明年初基础容量缩小到5G,执行这一步骤避免(保持30G)

    Onedrive作为微软的云盘,相当实用,存储一些照片文档. 之前微软一直执行免费用户可以永久拥有30G空间(基础+开启功能获得). 但微软打算从明年开始减低这一优惠至5G. 不过最近微软有一些放松, ...

  3. break into Ubuntu System

    This morning, I got a spare machine from of of the labmates. The OS is ubuntu 12.04. I could not log ...

  4. sqlserver查看所有的外键约束

    select a.name as 约束名, object_name(b.parent_object_id) as 外键表, d.name as 外键列, object_name(b.reference ...

  5. The Nine Indispensable Rules for HW/SW Debugging 软硬件调试之9条军规

    I read this book in the weekend, and decided to put the book on my nightstand. It's a short and funn ...

  6. svn co

    svn  co  的用法经常有两种:    第一种:  直接  svn  co    http://svnserver/mypro/trunk                 此时, 会在你的当前目录 ...

  7. Android AsyncTask异步任务(二)

    之前我们讲过了AsyncTask 的生命周期(onPreExecute-->doInBackground-->onProgressUpdate-->onPostExecute),今天 ...

  8. Python的平凡之路(13)

    一.Python的paramiko模块介绍 Python 的paramiko模块,该模块和SSH用于连接远程服务器并执行相关操作 SSH client 用于连接远程服务器并执行基本命令 基于用户名和密 ...

  9. HTML5 <video> - 使用 DOM 进行控制

    HTML5 <video> 元素同样拥有方法.属性和事件. 其中的方法用于播放.暂停以及加载等.其中的属性(比如时长.音量等)可以被读取或设置.其中的 DOM 事件能够通知您,比方说,&l ...

  10. android使用镜像 Android sdk 和源码等

    HTTP Proxy Server:mirrors.neusoft.edu.cn HTTP Proxy Port :80