Showing phone contact on listview

on Saturday, January 5, 2013 | 5:53 PM

Showing phone contact on listview. Listview serves to accommodate the names of contact (phone book). Preview the end result of this exercise looks like Picture 1.

contactTOlistview

The following steps:

1. Create new project

Project Name : contactList
Package Name : com.concact.list
Activity Name : ContactListActivity
Target Device : Andorid 2.3

2. Add widgets ListView on activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
3. The core of is contactListActivity.java
ContactListActivity
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ContactListActivity extends Activity {
ListAdapter adapter;
String[] item, number;
ListView listView;
Cursor mCursor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
readContacts();
}

public void readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);

if (cur.getCount() > 0) {
int i = 0;
int j = 0;
item = new String[cur.getCount()];
number = new String[cur.getCount()];
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
item[i++] = name;
// get the phone number
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);

while (pCur.moveToNext()) {
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
number[j++] = phone;
}
pCur.close();

}
}
ArrayAdapter<String> g = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, item);
listView.setAdapter(g);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String v = number[arg2];
Toast.makeText(getApplicationContext(), v,
Toast.LENGTH_SHORT).show();
}
});
}
}

}
4. Add some user permissions in AndroidManifest.xml (line 8)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.contact.list" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.READ_CONTACTS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ContactListActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Light"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Completed. Congratulations creative with android!
Share this post :
Showing phone contact on listview Published By Technoledge Terima Kasih Telah membaca Showing phone contact on listview
Link Published on: 2013-01-05T17:53:00+07:00
Showing phone contact on listview. Listview serves to accommodate the names of contact (phone book). Preview the end result of this exercise... Rating 5 ★★★★★ Reviews 1110
Index »

Android App

 
Copyright © 2013. Technoledge - All Rights Reserved
Proudly powered by Blogger