android 读取联系人实例
2015-11-01 20:35
477 查看
<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);">contentProvider的应用,读取本地联系人,(id,name,电话号码)以listView显示。</span>
<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);">布局很简单,一个listview.</span>
<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"><span style="font-size:24px;"><strong><em><u>activity_main.xml</u></em></strong></span></span>
<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: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.fxj.xml.jiexi.MainActivity" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </RelativeLayout>item_listview.xml
<span style="font-size:18px;"><u><</u>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:orientation="horizontal" android:padding="5dp" tools:context="com.fxj.xml.jiexi.MainActivity" > <TextView android:id="@+id/tv_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:gravity="center" android:layout_weight="1" /> <TextView android:layout_weight="1" android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小名" android:gravity="center" /> <TextView android:layout_weight="1" android:id="@+id/tv_age" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="12" /><u> </u> </LinearLayout></span><u style="font-size:24px; font-weight: bold; font-style: italic;"> </u>java文件:MainActivity.java
<span style="font-size:18px;">package comfxj.readcontacts; import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.support.v7.app.ActionBarActivity; import android.widget.ListView; public class MainActivity extends ActionBarActivity { private Uri contactUri = ContactsContract.Contacts.CONTENT_URI; private MyAdapter adapter; private List<Contact> contacts; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); getdatas(); adapter = new MyAdapter(contacts, this); listView.setAdapter(adapter); } private void getdatas() { contacts = getQueryData(); } private List<Contact> <strong>getQueryData() </strong>{ List<Contact> contacts = new ArrayList<Contact>(); <strong>// 获取ContentResolver对象 ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(contactUri, null, null, null, null);</strong> // 获得_ID字段的索引 int idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID); // 获取Name字段的索引 int nameIndex = cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); // 遍历Cursor提取数据 while (cursor.moveToNext()) { Contact contact = new Contact(); <strong>// 获取联系人id、姓名 String id = cursor.getString(idIndex); String name = cursor.getString(nameIndex);</strong> contact.setId(id); contact.setName(name); <strong>// 获取联系人的对应电话号码 String ContactId = cursor.getString(idIndex); Cursor phone = resolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null); while (phone.moveToNext()) { String number = phone .getString(phone .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // 电话号码赋值 contact.setPhoneNumber(number);</strong> } contacts.add(contact); } cursor.close(); return contacts; } } </span>
简单的adaper
MyAdapter.java
<span style="font-size:18px;">package comfxj.readcontacts; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private List<Contact> contacts; private Context context; private LayoutInflater inflater; public MyAdapter(List<Contact> contacts,Context context){ this.contacts = contacts; this.context = context; inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return contacts.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return contacts.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater .inflate(R.layout.item_listview, null, false); } TextView tvId = (TextView) convertView.findViewById(R.id.tv_id); TextView tvName = (TextView) convertView .findViewById(R.id.tv_name); TextView tvNumber = (TextView) convertView .findViewById(R.id.tv_age); tvId.setText(contacts.get(position).getId()); tvName.setText(contacts.get(position).getName()); tvNumber.setText(contacts.get(position).getPhoneNumber()); return convertView; } }</span><u style="font-size:24px; font-weight: bold; font-style: italic;"> </u>
<span style="font-size:18px;"> </span>
<span style="font-size:18px;"> </span>
装数据的bean类:
Contact.java
<span style="font-size:18px;">package comfxj.readcontacts; public class Contact { private String id; private String name; private String phoneNumber; public String getId() { return id; } public void setId(String 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; } }</span><u style="font-size:24px; font-weight: bold; font-style: italic;"> </u>运行效果:
结束。有点粗糙啦
相关文章推荐
- 观察者模式(Observer)在Android中的应用:
- android自定义控件(理论知识学习 +自定义属性的讲解)
- Android Debug Bridge(adb)
- Xamarin.Android支付宝SDK Demo
- Android中使用ShareSDK实现分享
- android 中 sqlite sql语句 参数 分析
- Android视频播放框架——封装FFMPEG的Vitamio
- android自定义带有布局视图的对话框
- android 72 确定取消对话框,单选对话框,多选对话框
- Android开发Toast样式总结
- android 调试常用命令
- Android消息处理机制--Looper,Handler,Message
- android 71 ArrayAdapter和SimpleAdapter
- Android四大组件之Service(服务)(二)
- Fragment介绍:
- [转]android系统各种音量的获取与设置
- Android图片加载(2)
- 拼图小游戏”ST--拼图“开发篇之主要功能的实现(二)
- Android笔记(五十) Android中的JSON数据
- android 基础的小问题1(Adapter的数据无法刷新)