IT프로그래밍

안드로이드 ) 전화번호부 가져오기

Manniz 2013. 7. 26.

메인부분

package com.example.list_view4;


import android.app.ListActivity;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.support.v4.widget.SimpleCursorAdapter;

import android.view.Menu;

import android.widget.ListAdapter;


public class MainActivity extends ListActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Cursor contactsCursor = this.managedQuery(ContactsContract.Contacts.CONTENT_URI,

null, null, null, null);

startManagingCursor(contactsCursor);

String[] columns = new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};

int[] ids = new int[] {android.R.id.text1};

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, contactsCursor, columns, ids);

setListAdapter(adapter);

}


@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;

}


}


레이아웃 부분

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    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=".MainActivity" >


    

    <LinearLayout

        android:layout_width="fill_parent"

   android:layout_height="wrap_content"

   android:orientation="vertical">

   

        <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TEST" />

   

        

        

        </LinearLayout>

        

        <ListView

android:id="@android:id/list"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        

        />

    


</LinearLayout>


메니페스트 부분

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

</manifest>



댓글