Search

'listview'에 해당되는 글 1건

  1. 2013.08.09 Android 전화번호부 목록 ListView에 띄우기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.tistory.tansanc.Test130805;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c09_listview);

		// * 데이터 원본 준비
		ArrayList arContactList = new ArrayList();		
		arContactList = getContactList();
		ArrayList arGeneral = new ArrayList();
		for( int i = 0 ; i < arContactList.size() ; i++ )
		{
			arGeneral.add( arContactList.get(i).name );
		}
		// */

		/*
		 * 배열로 준비 String[] arGeneral = {"김유신", "이순신", "강감찬", "을지문덕"}; //
		 */

		// 어댑터 준비
		ArrayAdapter Adapter;
		Adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, arGeneral);

		// 어댑터 연결
		ListView list = (ListView) findViewById(R.id.list);
		list.setAdapter(Adapter);
	}

	private ArrayList getContactList() {

		Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

		String[] projection = new String[] {

		ContactsContract.CommonDataKinds.Phone.CONTACT_ID, // 연락처 ID -> 사진 정보
															// 가져오는데 사용

				ContactsContract.CommonDataKinds.Phone.NUMBER, // 연락처

				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }; // 연락처
																		// 이름.

		String[] selectionArgs = null;

		String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

		+ " COLLATE LOCALIZED ASC";

		Cursor contactCursor = managedQuery(uri, projection, null,

		selectionArgs, sortOrder);

		ArrayList contactlist = new ArrayList();

		if (contactCursor.moveToFirst()) {

			do {

				String phonenumber = contactCursor.getString(1).replaceAll("-",

				"");

				if (phonenumber.length() == 10) {

					phonenumber = phonenumber.substring(0, 3) + "-"

					+ phonenumber.substring(3, 6) + "-"

					+ phonenumber.substring(6);

				} else if (phonenumber.length() > 8) {

					phonenumber = phonenumber.substring(0, 3) + "-"

					+ phonenumber.substring(3, 7) + "-"

					+ phonenumber.substring(7);

				}

				Contact acontact = new Contact();

				acontact.setPhotoid(contactCursor.getLong(0));

				acontact.setPhonenum(phonenumber);

				acontact.setName(contactCursor.getString(2));

				contactlist.add(acontact);

			} while (contactCursor.moveToNext());

		}

		return contactlist;

	}

	public class Contact {
		long photoid;
		String phonenum;
		String name;
		
		public long getPhotoid() {
			return photoid;
		}
		public void setPhotoid(long photoid) {
			this.photoid = photoid;
		}
		public String getPhonenum() {
			return phonenum;
		}
		public void setPhonenum(String phonenum) {
			this.phonenum = phonenum;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		
	}


}

Layout XML

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
 android:id="@+id/list" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />
</LinearLayout>

 

Manifest 추가

 

<uses-permission android:name="android.permission.READ_CONTACTS" />

'Programming > Android' 카테고리의 다른 글

Android 라디오 mms 주소로 듣기  (1) 2013.08.16
Android Intro 로딩 Activity  (0) 2013.08.12
안드로이드 타자 연습 예제  (0) 2013.08.08
Android 다양한 Layout 사용법  (0) 2013.08.07
Could not find .apk  (0) 2013.05.24