获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。
package com.lixu.tongxunlu; import java.util.ArrayList;
import com.lixu.tongxunlu.PinnedSectionListView.PinnedSectionListAdapter;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView; public class MainActivity extends Activity {
private ArrayList<Contacts> contacts;
private ArrayList<Data> data; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去掉标题头
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 设置程序全屏
toggleFullscreen(true); data = new ArrayList<Data>();
contacts = new ArrayList<Contacts>();
// 获取手机联系人后装到contacts中
getcontacts(contacts); int A = (int) 'A';
for (int i = 0; i < 26; i++) {
int letter = A + i;
char c = (char) letter; Data group = new Data(); group.type = Data.GROUP;
group.text = c + "";
data.add(group);
for (int j = 0; j < contacts.size(); j++) {
Contacts cc = contacts.get(j);
String ss = cc.firstLetterofNmae();
// 判断联系人名字首字母是否和组名字母一样,一样后加入本组
if (ss.equals(group.text)) {
Data child = new Data();
child.type = Data.CHILD;
child.text = cc.name + "" + cc.getPhoneNumbers();
child.contacts = cc;
data.add(child);
} } } PinnedSectionListView pslv = (PinnedSectionListView) findViewById(R.id.pslv); pslv.setAdapter(new MyAdapter(this, -1)); pslv.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 判断点击子菜单才会触发点击事件
if (data.get(position).type == Data.CHILD) {
Data datas = data.get(position);
Contacts contact = datas.contacts;
// 获取电话号码
String number = contact.getPhoneNumbers(); Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
startActivity(intent);
}
}
}); } // 设置程序全屏的方法
public void toggleFullscreen(boolean fullScreen) {
// fullScreen为true时全屏 WindowManager.LayoutParams attrs = getWindow().getAttributes(); if (fullScreen) {
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
} else {
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
} getWindow().setAttributes(attrs);
} private class Data {
public static final int GROUP = 0;
public static final int CHILD = 1;
public static final int STYE = 2;
public String text;
public int type;
public Contacts contacts = null; } private class MyAdapter extends ArrayAdapter implements PinnedSectionListAdapter {
private LayoutInflater flater; public MyAdapter(Context context, int resource) {
super(context, resource);
flater = LayoutInflater.from(context);
} @Override
public int getCount() {
return data.size();
} @Override
public Object getItem(int position) {
return data.get(position);
} @Override
public int getItemViewType(int position) {
return data.get(position).type;
} @Override
public int getViewTypeCount() {
return Data.STYE;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
Data data = (Data) getItem(position);
switch (type) {
case Data.GROUP:
if (convertView == null)
convertView = flater.inflate(android.R.layout.simple_list_item_1, null); TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
tv.setBackgroundColor(Color.GREEN);
tv.setTextSize(35);
tv.setText(data.text); break; case Data.CHILD:
if (convertView == null)
convertView = flater.inflate(android.R.layout.simple_list_item_1, null); TextView tv1 = (TextView) convertView.findViewById(android.R.id.text1);
tv1.setTextSize(20);
tv1.setText(data.text); break; default:
break;
}
return convertView;
} @Override
public boolean isItemViewTypePinned(int viewType) {
boolean type = false;
switch (viewType) {
case Data.CHILD: type = false; break; case Data.GROUP: type = true; break; default:
break;
}
return type;
} } // 获取手机联系人的方法
private void getcontacts(ArrayList<Contacts> contacts) {
Uri uri = Uri.parse("content://com.android.contacts/contacts");
ContentResolver resolver = this.getContentResolver();
// 给query传递一个SORT_KEY_PRIMARY,让ContentResolver将获得的数据按照联系人名字首字母排序
Cursor cursor = resolver.query(uri, null, null, null,
android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY);
while (cursor.moveToNext()) {
// 联系人的id
String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
// 将联系人按姓名首字母分组
String sort_key_primary = cursor
.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY));
// 获取联系人的名字
String name = cursor
.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME));
Contacts mContacts = new Contacts();
mContacts.id = id;
mContacts.name = name;
mContacts.sort_key_primary = sort_key_primary;
// 获取联系人手机号码
Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null);
// 将获取到的号码遍历
ArrayList<String> phonenumbers = new ArrayList<String>();
while (phone.moveToNext()) {
int phoneFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phonenumber = phone.getString(phoneFieldColumnIndex);
phonenumbers.add(phonenumber);
}
mContacts.phonenumbers = phonenumbers;
contacts.add(mContacts); }
} private class Contacts {
public String id;
public String name;
public String sort_key_primary;
public ArrayList<String> phonenumbers; // 获取汉字首字母转换成大写字母
public String firstLetterofNmae() {
String s = sort_key_primary.charAt(0) + "";
return s.toUpperCase(); } public String getPhoneNumbers() {
String phones = "";
for (int i = 0; i < phonenumbers.size(); i++) {
phones += ":" + phonenumbers.get(i);
}
return phones; }
} }
xml文件:
不要忘记添加各种权限。
<RelativeLayout 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:padding="10dp" > <com.lixu.tongxunlu.PinnedSectionListView
android:id="@+id/pslv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </RelativeLayout>
效果示例图:
获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。的更多相关文章
- mysql、oracle 中按照拼音首字母排序
mysql中按照拼音首字母排序 convert(name using gbk) ASC 注:name 为字段名称 oracle中按照拼音首字母排序 nlssort(enterprise_name,'N ...
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...
- java通过文件路径读取该路径下的所有文件并将其放入list中
java通过文件路径读取该路径下的所有文件并将其放入list中 java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...
- .Net中把图片等文件放入DLL中,并在程序中引用
原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...
- iOS中获取本地通讯录联系人以及汉字首字母排序
iOS中获取手机通讯录中的联系人信息: /*** 加载本地联系人*/ - (void)loadLocalContacts { //新建一个通讯录类 ABAddressBookRef addressBo ...
- set是无序集合,放入set中的元素通过iterator输出时候是无序的
set是无序集合,放入set中的元素通过iterator输出时候是无序的 HashMap<String , String> hashMap = new HashMap<String ...
- tuple放入dict中
tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...
- 在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可
在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可
- 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中
前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...
随机推荐
- iOS - OC SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 特点: 在内存中只有一个实例 提供一个全局的访问点 ...
- Android_相关路径
1. Android应用安装涉及到如下几个目录:system/app 系统自带的应用程序,无法删除.data/app 用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.da ...
- spring 好处与优点
使用Spring有什么好处?(1)Spring能有效地组织你的中间层对象.(2)Spring能消除在许多工程中常见的对Singleton的过多使用.(3)Spring能消除各种各样自定义格式的属性文件 ...
- [html] HTML结构的语义化
原文链接:http://www.cnblogs.com/freeyiyi1993/p/3615179.html 1.什么是html语义化 选择合适的html标签,便于开发者阅读和写出更优雅的代码的同时 ...
- laravel各种路径的获取方法
若Route中有Route::get('home/test', 'HomeController@index')->name('test'); ①视图中的href跳转 一.<a href=& ...
- 工作流学习——Activiti整体认识二步曲 (zhuan)
http://blog.csdn.net/zwk626542417/article/details/46594505 ***************************************** ...
- OpenGL的几何变换[转]
OpenGL的几何变换 1.实验目的: 理解掌握一个OpenGL程序平移.旋转.缩放变换的方法. 2.实验内容: (1)阅读实验原理,运行示范实验代码,掌握OpenGL程序平移.旋转.缩放变换的方法: ...
- xcode使用
1xcode模拟器插件路径 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins 2调试 Po值: nil就是0,而不是空值 小细 ...
- js encodeURI方法认识
很早就知道js中encodeURI方法,也很早就用过,但是每次看到它总感觉有些陌生,因为不知道到底是什么原理,和普通的编码到底什么关系, 今天在查看w3c api时又遇到了她,正好有空就多看了几眼,突 ...
- centos7命令
查看ip ip addr ip link 添加服务 systemctl enable nginx 添加防火墙端口 firewall-cmd --zone=public --add-port=80/tc ...