本文主要是介绍getAllContants--获取手机联系人列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
配置文件
activity_main.xml(布局文件)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><EditTextandroid:id="@+id/et_main_number"android:layout_width="match_parent"android:layout_height="wrap_content"></EditText><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="选择联系人" android:onClick="selectPersons"/>
</LinearLayout>
activity_second.xml(listview布局)
<ListView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/lv_list_persons" >
</ListView>
person_item.xml(每个联系人条目布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_item_person_name"android:layout_width="100dp"android:layout_height="wrap_content"android:text="TextView" android:textSize="20sp"/><TextViewandroid:id="@+id/tv_item_person_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" android:textSize="20sp"/>
</LinearLayout>
MainActivity.java
package com.example.l09_app;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;public class MainActivity extends Activity {private EditText et_main_number;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取号码框et_main_number = (EditText) findViewById(R.id.et_main_number);}public void selectPersons(View view) {// 带回调的启动startActivityForResult(new Intent(this, PersonListActivity.class), 1);}// 数据的回显@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == 1 && resultCode == 2) {String number = data.getStringExtra("number");et_main_number.setText(number);}}
}
PersonListActivity.java
package com.example.l09_app;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;public class PersonListActivity extends Activity implements OnItemClickListener {ListView lv_list_persons;// 成员变量初始化data,后面内部类中使用private List<Map<String, String>> data = new ArrayList<Map<String, String>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);// 获得lv_list_persons---listview视图lv_list_persons = (ListView) findViewById(R.id.lv_list_persons);// 准备数据---content://com.android.contacts/data/phonesUri uri = Phone.CONTENT_URI;String[] projection = { Phone.DISPLAY_NAME, Phone.NUMBER };Cursor cursor = getContentResolver().query(uri, projection, null, null,null);while (cursor.moveToNext()) {String name = cursor.getString(0);String number = cursor.getString(1);Map<String, String> map = new HashMap<String, String>();map.put(Phone.DISPLAY_NAME, name);map.put(Phone.NUMBER, number);data.add(map);}cursor.close();// 设置适配器lv_list_persons.setAdapter(new PersonAdapter());// 添加item的点击事件lv_list_persons.setOnItemClickListener(this);}// 添加item的点击事件@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {// 取数据String number = data.get(position).get(Phone.NUMBER);Intent intent = getIntent();intent.putExtra("number", number);// 设置要回传的数据setResult(2, intent);// 关闭当前界面finish();}// 适配器private class PersonAdapter extends BaseAdapter {@Overridepublic int getCount() {return data.size();}@Overridepublic Object getItem(int position) {return data.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null;// 如果没有视图可以复用时候,就在本activity视图动态加载视图文件if (convertView == null) {// 动态加载视图convertView = View.inflate(PersonListActivity.this,R.layout.person_item, null);viewHolder = new ViewHolder();viewHolder.tv_item_person_name = (TextView) convertView.findViewById(R.id.tv_item_person_name);viewHolder.tv_item_person_number = (TextView) convertView.findViewById(R.id.tv_item_person_number);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}// 获取值Map<String, String> map = data.get(position);viewHolder.tv_item_person_name.setText(map.get(Phone.DISPLAY_NAME));viewHolder.tv_item_person_number.setText(map.get(Phone.NUMBER));return convertView;}// 适配器的内部类class ViewHolder {public TextView tv_item_person_name;public TextView tv_item_person_number;}}
}
这篇关于getAllContants--获取手机联系人列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!