RSS订阅优然探索
你的位置:首页 » JAVA » 正文

Android获取联系人

选择字号: 超大 标准 发布时间:2013-4-24 17:7:35 | 作者:admin | 0个评论 | 人浏览

 package com.lc.product.ui.user;

 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
 
import com.lc.product.R;
import com.lc.product.event.ListViewContactsAdapter;
import com.lc.product.event.ListViewContactsAdapter.onCheckBoxClickListener;
import com.lc.product.ui.AppActivity;
 
public class UserContactsActivity extends AppActivity {
 
private final String[] SettingList = { "漏话消息", "信箱消息", "设置消息", "系统消息", };
private Integer[] SettingImages = { 0, 0, 0, 0 };
private Integer[] Rightimage = { R.drawable.msgtypedot,
R.drawable.msgtypedot, R.drawable.msgtypedot, R.drawable.msgtypedot };
private final String[] Rightstring = { "4", "3", "2", "1" };
// private ListView mListView;
private List<Map<String, Object>> listItems1;
private ListView listView1;
private ListViewContactsAdapter listViewAdapter1;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_user_contacts);
listView1 = (ListView) findViewById(R.id.list_contacts1);
btnSelectAll = (Button) findViewById(R.id.contacts_selectall);
btnSubmit = (Button) findViewById(R.id.contacts_submit);
getPhoneContacts();
setEvent();
}
 
private void setData() {
listItems1 = new ArrayList<Map<String, Object>>();
 
for (int i = 0; i < 4; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", SettingImages[i]);
map.put("title", SettingList[i]);
map.put("rightimage", Rightimage[i]);
map.put("rightstring", Rightstring[i]);
map.put("key", "key" + i);
map.put("right", "right" + i);
listItems1.add(map);
}
 
}
 
private View layout_user_contacts_footView;
private Button btnSelectAll, btnSubmit;
 
private void setListViewFoot() {
//
// layout_user_contacts_footView = getLayoutInflater().inflate(
// R.layout.layout_user_contacts_foot, null);
// listView1.addFooterView(layout_user_contacts_footView); // 设置列表底部视图
// btnSelectAll = (Button) layout_user_contacts_footView
// .findViewById(R.id.contacts_selectall);
// btnSubmit = (Button) layout_user_contacts_footView
// .findViewById(R.id.contacts_submit);
 
btnSelectAll.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
 
String sKey = btnSelectAll.getText().toString();
boolean b = true;
if (sKey.equals("全选")) {
b = true;
btnSelectAll.setText("全不选");
 
} else {
b = false;
btnSelectAll.setText("全选");
 
}
 
int iCount = listItems1.size();
Log.i("listItems1", "iCount=" + iCount);
for (int i = 0; i < iCount; i++) {// 遍历mGridView子控件找到被选中的checkbox的id
Log.i("listItems1", "i=" + i);
LinearLayout layout = (LinearLayout) listView1
.getChildAt(i);
if (null != layout) {
CheckBox checkBox = (CheckBox) layout
.findViewById(R.id.chkcontacts);
if (null != checkBox) {
checkBox.setChecked(b);
}
}
listViewAdapter1.setCheck(i, b);
}
JudgeCountButton();
}
 
});
 
}
 
private void JudgeCountButton() {
 
int iCount = listViewAdapter1.isSelected.size();
int j = 0;
for (int i = 0; i < iCount; i++) {
if (listViewAdapter1.isSelected.get(i)) {
j++;
}
}
if (j == 0) {
btnSubmit.setText("确认");
 
} else {
btnSubmit.setText("确认(" + j + ")");
}
}
 
private void setEvent() {
// listItems1=getListItems();
listViewAdapter1 = new ListViewContactsAdapter(this, listItems1); // 创建适配器
setListViewFoot();
listViewAdapter1.setonCheckBoxClickListener(new onCheckBoxClickListener() {
@Override
public void oncheckBoxclickListenr(int position) {
// TODO Auto-generated method stub
JudgeCountButton();
}
});
listView1.setAdapter(listViewAdapter1);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
CheckBox chk = (CheckBox) arg1.findViewById(R.id.chkcontacts);
if (chk != null) {
if (chk.isChecked()) {
chk.setChecked(false);
} else {
chk.setChecked(true);
}
listViewAdapter1.setCheck(arg2, chk.isChecked());
JudgeCountButton();
}
 
}
});
}
 
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID };
 
private void getPhoneContacts() {
listItems1 = new ArrayList<Map<String, Object>>();
ContentResolver resolver = this.getContentResolver();
 
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null);
 
int i = 1;
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
 
// 得到手机号码
String phoneNumber = phoneCursor.getString(1);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor.getString(0);
if (contactName.length() > 5) {
contactName = contactName.substring(0, 5) + "..";
}
 
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", 0);
map.put("title", contactName);
map.put("rightimage", 0);
map.put("rightstring", phoneNumber);
map.put("key", "key" + phoneNumber);
map.put("right", "right" + phoneNumber);
listItems1.add(map);
i++;
if (i > 50) {
break;
}
 
}
 
phoneCursor.close();
}
}
 
}
 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。