Lo@dinG Administrator
Jumlah posting : 434 Points : 874 Reputation : 22 Join date : 13.12.10 Age : 35 Lokasi : Dimana-mana ???
| Subyek: Menampilkan Kontak telepon pada listview (Widget) Sat Dec 22, 2012 3:36 pm | |
| ok ane udah lama kagak nongol lg di forum tercinta.., kali ini ane coba share ilmu yg ane dpt dr site tetangga.., dan ane juga udah coba praktekin. klw gitu lang sung aja dehh.., buat teman" yg mau belajar buat app android :D mari Om buat project baru! 1. Create new project Project Name : contactList Package Name : com.concact.list Activity Name : ContactListActivity Target Device : Andorid 2.3 2. Tambahkan widget Listview pada activity_main.xml - Code:
-
<?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. Berikut ini bagian intinya yaitu contactListActivity.java - Code:
-
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. Tambahkan beberapa user permission pada AndroidManifest.xml (baris 8) - Code:
-
<?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 OK Selesai Happy Codding | |
|