码迷,mamicode.com
首页 > 其他好文 > 详细

SimpleAdapter应用(显示联系人)

时间:2015-08-08 16:22:42      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

1.创建一个android project

  1.1添加SimpleCursorAdapterActivity类,并在AnddroidMainfest.xml添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wang.adapder" >
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SimpleCursorAdapterActivity" >
        </activity>
    </application>

</manifest>

  1.2在MainActivity类的onCreate方法中添加如下代码:

  Button btn = (Button)findViewById(R.id.button_simpCursorAdapter);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(MainActivity.this,SimpleCursorAdapterActivity.class);
                startActivity(intent);
            }
        });

2.创建activity_simple_cursor.xml和activity_list_item.xml两个Layout XML

  在activity_simple_cursor.xml中添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示手机的联系人:"
        />
    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        />
</LinearLayout>

  在activity_list_item.xml中添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" android:layout_height="match_parent">
<TableRow android:id="@+id/TableRow0"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    <TextView android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20px" />
    <TextView android:text=": "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20px" />
    <TextView android:id="@+id/item2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20px" />
</TableRow>
</LinearLayout>

3.在SimpleCursorAdapterActivity添加如下代码:

技术分享
package com.example.wang.adapder;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Wang on 2015/8/8.
 */
public class SimpleCursorAdapterActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle saveInstance){
        super.onCreate(saveInstance);

      setContentView(R.layout.activity_simple_cursor_adapter);
        List<HashMap<String,String>> list = fillMaps();
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.activity_list_item,new String []{"name","key"},new int [] {R.id.item,R.id.item2});
        setListAdapter(adapter);
    }


    private List<HashMap<String, String>> fillMaps() {
        List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();

        Cursor cur = null;
        try {
            // Query using ContentResolver.query or Activity.managedQuery
            cur = getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            if (cur.moveToFirst()) {
                //判断是否有联系人
                int idColumn = cur.getColumnIndex(
                        ContactsContract.Contacts._ID);
                int displayNameColumn = cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME);
                // Iterate all users
                do {
                    String contactId;
                    String displayName;//
                    String phoneNumber = "";//电话号码
                    // Get the field values
                    contactId = cur.getString(idColumn);//ID
                    displayName = cur.getString(displayNameColumn);//名称
                    // Get number of user‘s phoneNumbers
                    int numberCount = cur.getInt(cur.getColumnIndex(
                            ContactsContract.Contacts.HAS_PHONE_NUMBER));//获取电话号码长度
                    if (numberCount>0) {
                        //电话号码存在
                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = " + contactId
                                /*+ " and " + ContactsContract.CommonDataKinds.Phone.TYPE
                                + "=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE*/,
                                null, null);//根据ID查询联系人
                        if (phones.moveToFirst()) {
                            int numberColumn = phones.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER);//获取第一个电话号码
                            // Iterate all numbers
                            do {
                                phoneNumber += phones.getString(numberColumn) + ",";//以逗号分隔显示其它的电话号码
                            } while (phones.moveToNext());
                        }
                    }
                    // Add values to items
                    HashMap<String, String> i = new HashMap<String, String>();
                    i.put("name", displayName);//名称
                    i.put("key", phoneNumber);//号码
                    items.add(i);
                } while (cur.moveToNext());
            } else {
                //如果没有联系人添加说明记录
                HashMap<String, String> i = new HashMap<String, String>();
                i.put("name", "通讯录:");
                i.put("key", "没人联系人");
                items.add(i);
            }
        } finally {
            if (cur != null)
                cur.close();
        }
        return items;
    }



}
读取联系人并显示成ListView代码

注意:不要忘了在AndroidMainfest.xml中添加读取联系人的权限:<uses-permission android:name="android.permission.READ_CONTACTS"/>

4.测试的结果如下:

技术分享

 

SimpleAdapter应用(显示联系人)

标签:

原文地址:http://www.cnblogs.com/wangjunqiao/p/4713227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!