代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
读取联系人姓名、号码
作者:
Dezai.CN
/ 发布于
2012/9/19
/
599
读取联系人姓名、号码,并lisetview 显示,获取listview数据,并发短信、或者拨号
<div>package com.android.hello;</div> import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.telephony.PhoneNumberUtils; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import android.provider.ContactsContract;</div> import java.util.ArrayList; import java.util.HashMap; import android.widget.SimpleAdapter;</div> <div>@SuppressWarnings("deprecation") public class hello extends Activity { /** Called when the activity is first created. */ // @SuppressWarnings("deprecation") // @Override // private static final String TAG="App"; ListView listView; ListAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); LinearLayout linearLayout=new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setBackgroundColor(Color.BLACK); LinearLayout.LayoutParams param=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); listView=new ListView(this); listView.setBackgroundColor(Color.BLACK); linearLayout.addView(listView,param); this.setContentView(linearLayout); //生成动态数组,加入数据 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); ArrayList<HashMap<String, Object>> listItemRead = new ArrayList<HashMap<String, Object>>(); Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); while (cursor.moveToNext()) { HashMap<String, Object> map = new HashMap<String, Object>(); String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); map.put("ItemTitle", phoneName);//电话姓名 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (hasPhone.compareTo("1") == 0) { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); while (phones.moveToNext()) { String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String phoneTpye = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); map.put("ItemText", phoneNumber); // 多个号码如何处理 Log.d(TAG,"testNum="+ phoneNumber + "type:"+phoneTpye); } phones.close(); } Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null); while (emails.moveToNext()) { String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); String emailType = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));</div> <div> Log.d(TAG,"testNum="+ emailAddress + "type:"+emailType); } emails.close(); listItem.add(map); } //生成适配器的Item和动态数组对应的元素 SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源 android.R.layout.simple_list_item_2,//ListItem的XML实现 //动态数组与ImageItem对应的子项 new String[] {"ItemTitle", "ItemText"}, //ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] {android.R.id.text1,android.R.id.text2} ); listView.setAdapter(listItemAdapter); cursor.close(); //listView.setEmptyView(findViewById(R.id.empty)); listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub //openToast("滚动到:"+arg0.getSelectedItemId()); //短信发送 setTitle("选择"+arg2+"项目"); openToast("选择"+arg0.getSelectedItemId()+"项目"); RelativeLayout lr = (RelativeLayout) arg1; TextView mText = (TextView) lr.getChildAt(1); openToast(mText.getText().toString());</div> <div> String number = mText.getText().toString(); Log.d(TAG, "number=" + number); // 判断电话号码的有效性 if (PhoneNumberUtils.isGlobalPhoneNumber(number)) { Intent intent = new Intent(Intent.ACTION_SENDTO, Uri .parse("smsto://" + number)); intent.putExtra("sms_body", "The SMS text"); startActivity(intent); } } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){</div> <div> public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub // openToast("Click"+Integer.toString(position+1)+"项目"); RelativeLayout lr = (RelativeLayout) arg1; TextView mText = (TextView) lr.getChildAt(1); openToast(mText.getText().toString());</div> <div> String number = mText.getText().toString(); Log.d(TAG, "number=" + number); // 判断电话号码的有效性 if (PhoneNumberUtils.isGlobalPhoneNumber(number)) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri .parse("tel://" + number)); startActivity(intent); } } }); } private void openToast(String str){ Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); } } </div>
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3