본문 바로가기

Android

Android 주소록을 불러와서 이름순으로 정열

MainActivity

 


package com.example.number;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity
{
     String tag = null;
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          String disname = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
          String number = ContactsContract.CommonDataKinds.Phone.NUMBER;
          String type = ContactsContract.CommonDataKinds.Phone.TYPE;
          Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
          Cursor cursor2 = getContentResolver().query(uri,  new String[]{
    disname, number, type}, null,null,disname);
          int end = cursor2.getCount();     //전화번호부의 갯수 세기
          Log.d(tag, "end = "+end);
  
          String [] name = new String[end];  //전화번호부의 이름을 저장할 배열 선언
          int count = 0;
  
          if(cursor2.moveToFirst())
          {
               do
               {
                    name[count] = cursor2.getString(0)+":"+cursor2.getString(1);
                    count++;
               } while(cursor2.moveToNext() || count > end);
          }
          setListAdapter(new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_multiple_choice, name)); <--이 부분에서 name부분에 원래는 null값이 들어있다. 안드로이드에서 정렬 순서는 null 일경우에는 자동으로 숫자로 맞춰 정열이 되는데 이 부분에 name값을 넣어주면 이름순 대로 정열이 된다.
  
          final ListView listView = getListView();
          listView.setItemsCanFocus(false);
          listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 }

 

         @Override
         public boolean onCreateOptionsMenu(Menu menu) {
                  // Inflate the menu; this adds items to the action bar if it is present.
                  getMenuInflater().inflate(R.menu.main, menu);
                  return true;
         }

}

 

Manifest

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.number"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
      
        <activity
            android:name="com.example.number.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>
    </application>

</manifest>