Get contacts android java

How to read all contacts in android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

In the above code, we have taken list view.

Step 3 − Add the following code to src/MainActivity.java

 import android.annotation.TargetApi; import android.content.ContentResolver; import android.content.pm.PackageManager; import android.database.Cursor; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity < public static final int REQUEST_READ_CONTACTS = 79; ListView list; ArrayList mobileArray; @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) < mobileArray = getAllContacts(); >else < requestPermission(); >list = findViewById(R.id.list); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, mobileArray); list.setAdapter(adapter); > private void requestPermission() < if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_CONTACTS)) < // show UI part if you want here to show some rationale . >else < ActivityCompat.requestPermissions(this, new String[], REQUEST_READ_CONTACTS); > if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_CONTACTS)) < >else < ActivityCompat.requestPermissions(this, new String[], REQUEST_READ_CONTACTS); > > @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) < switch (requestCode) < case REQUEST_READ_CONTACTS: < if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) < mobileArray = getAllContacts(); >else < // permission denied,Disable the // functionality that depends on this permission. >return; > > > private ArrayList getAllContacts() < ArrayListnameList = new ArrayList<>(); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if ((cur ! = null ? cur.getCount() : 0) > 0) < while (cur ! = null && cur.moveToNext()) < String cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); nameList.add(name); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) >0) < Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[], null); while (pCur.moveToNext()) < String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); >pCur.close(); > > > if (cur ! = null) < cur.close(); >return nameList; > >

Step 4 − Add the following code to AndroidManifest.xml

Читайте также:  Ленивая загрузка java hibernate

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

Источник

How to get contacts in android and display all contacts in ListView

In this tutorial, we show you how to get all contacts information in Android, then display them in ListView. We will create a ‘Show Contacts’ Button widget. When user clicks the ‘Show Contacts’ button for fetch all phone, display name contacts information into ListView.
Follow the steps mentioned below to develop this application.

How to get contacts in android and display all contacts in ListView

Project Structure

Project Structure

Creating the Android Studio Project

Open Android Studio and create a new project with an empty activity called MainActivity.java

Creating the get contacts apps

Create New Project Android Read Contacts Example

selected the minimum SDK

Select the Empty activity

Leave the activity name “MainActivity”

Adding READ_CONTACTS Permissions in AndroidManifest.xml

Open AndroidManifest.xml file and add READ_CONTACTS permission inside it. This code snippet is shown below.

MainActivity.java

We create the requestContactPermission() method to to request read contact permission. Please view detail Android request contact permission Example. This code snippet is shown below.

public void requestContactPermission() < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) < if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_CONTACTS)) < AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Read contacts access needed"); builder.setPositiveButton(android.R.string.ok, null); builder.setMessage("Please enable access to contacts."); builder.setOnDismissListener(new DialogInterface.OnDismissListener() < @TargetApi(Build.VERSION_CODES.M) @Override public void onDismiss(DialogInterface dialog) < requestPermissions( new String[] , PERMISSIONS_REQUEST_READ_CONTACTS); > >); builder.show(); > else < ActivityCompat.requestPermissions(this, new String[], PERMISSIONS_REQUEST_READ_CONTACTS); > > else < getContacts(); >> else < getContacts(); >> @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) < switch (requestCode) < case PERMISSIONS_REQUEST_READ_CONTACTS: < if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) < getContacts(); >else < Toast.makeText(this, "You have disabled a contacts permission", Toast.LENGTH_LONG).show(); >return; > > >
private void getContacts()< ContentResolver contentResolver = getContentResolver(); String contactId = null; String displayName = null; contactsInfoList = new ArrayList(); Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); if (cursor.getCount() > 0) < while (cursor.moveToNext()) < int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); if (hasPhoneNumber >0) < ContactsInfo contactsInfo = new ContactsInfo(); contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); contactsInfo.setContactId(contactId); contactsInfo.setDisplayName(displayName); Cursor phoneCursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[], null); if (phoneCursor.moveToNext()) < String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contactsInfo.setPhoneNumber(phoneNumber); >phoneCursor.close(); contactsInfoList.add(contactsInfo); > > > cursor.close(); dataAdapter = new MyCustomAdapter(MainActivity.this, R.layout.contact_info, contactsInfoList); listView.setAdapter(dataAdapter); >

In the snippet code above, we get all contacts and to fill the ArrayList in each while loop add a ContactsInfo object to ArrayList. Then we set adapter with our custom layout xml.
Complete MainActivity.java Code

package com.jackrutorial.readcontactsexample; import android.annotation.TargetApi; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.database.Cursor; import android.os.Build; import android.provider.ContactsContract; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity < public static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1; MyCustomAdapter dataAdapter = null; ListView listView; Button btnGetContacts; ListcontactsInfoList; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnGetContacts = (Button) findViewById(R.id.btnGetContacts); listView = (ListView) findViewById(R.id.lstContacts); listView.setAdapter(dataAdapter); btnGetContacts.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < requestContactPermission(); >>); > private void getContacts()< ContentResolver contentResolver = getContentResolver(); String contactId = null; String displayName = null; contactsInfoList = new ArrayList(); Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); if (cursor.getCount() > 0) < while (cursor.moveToNext()) < int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); if (hasPhoneNumber >0) < ContactsInfo contactsInfo = new ContactsInfo(); contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); contactsInfo.setContactId(contactId); contactsInfo.setDisplayName(displayName); Cursor phoneCursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[], null); if (phoneCursor.moveToNext()) < String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contactsInfo.setPhoneNumber(phoneNumber); >phoneCursor.close(); contactsInfoList.add(contactsInfo); > > > cursor.close(); dataAdapter = new MyCustomAdapter(MainActivity.this, R.layout.contact_info, contactsInfoList); listView.setAdapter(dataAdapter); > public void requestContactPermission() < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) < if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_CONTACTS)) < AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Read contacts access needed"); builder.setPositiveButton(android.R.string.ok, null); builder.setMessage("Please enable access to contacts."); builder.setOnDismissListener(new DialogInterface.OnDismissListener() < @TargetApi(Build.VERSION_CODES.M) @Override public void onDismiss(DialogInterface dialog) < requestPermissions( new String[] , PERMISSIONS_REQUEST_READ_CONTACTS); > >); builder.show(); > else < ActivityCompat.requestPermissions(this, new String[], PERMISSIONS_REQUEST_READ_CONTACTS); > > else < getContacts(); >> else < getContacts(); >> @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) < switch (requestCode) < case PERMISSIONS_REQUEST_READ_CONTACTS: < if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) < getContacts(); >else < Toast.makeText(this, "You have disabled a contacts permission", Toast.LENGTH_LONG).show(); >return; > > > >

ContactsInfo.java

Create a ContactsInfo class under com.jackrutorial.readcontactsexample package and write the following code in it.

package com.jackrutorial.readcontactsexample; public class ContactsInfo < private String contactId; private String displayName; private String phoneNumber; public String getContactId() < return contactId; >public void setContactId(String contactId) < this.contactId = contactId; >public String getDisplayName() < return displayName; >public void setDisplayName(String displayName) < this.displayName = displayName; >public String getPhoneNumber() < return phoneNumber; >public void setPhoneNumber(String phoneNumber) < this.phoneNumber = phoneNumber; >>

Custom adapter Class

Create a MyCustomAdapter class under com.jackrutorial.readcontactsexample package and write the following code in it.

package com.jackrutorial.readcontactsexample; import android.content.Context; import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.TextView; import java.util.List; public class MyCustomAdapter extends ArrayAdapter  < private ListcontactsInfoList; private Context context; public MyCustomAdapter(@NonNull Context context, int resource, @NonNull List objects) < super(context, resource, objects); this.contactsInfoList = objects; this.context = context; >private class ViewHolder < TextView displayName; TextView phoneNumber; >@Override public View getView(int position, View convertView, ViewGroup parent) < ViewHolder holder = null; if (convertView == null) < LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = vi.inflate(R.layout.contact_info, null); holder = new ViewHolder(); holder.displayName = (TextView) convertView.findViewById(R.id.displayName); holder.phoneNumber = (TextView) convertView.findViewById(R.id.phoneNumber); convertView.setTag(holder); >else < holder = (ViewHolder) convertView.getTag(); >ContactsInfo contactsInfo = contactsInfoList.get(position); holder.displayName.setText(contactsInfo.getDisplayName()); holder.phoneNumber.setText(contactsInfo.getPhoneNumber()); return convertView; > > 

Design the Layouts

Create the contact_info.xml file under res/layout/ folder to the following.
Click on app > res > layout > Right Click on layout. Select New > XML > Layout XML File. In the dialog that appears, enter contact_info as name for the file. Then change the contact_info.xml layout to the following.

Create Contacts Layout

Run this application with the Android Emulator

  • Open Android Studio project and click Run.
  • In the Select Deployment Target dialog, select an existing emulator definition, and then click OK.
  • If you don’t see a definition you want to use, click Create New Virtual Device to launch the AVD Manager. After you define a new AVD, in the Select Deployment Target dialog (Android 6.0 API level 23 or higher), click OK.
  • If you want to use this emulator definition as the default for your project, select Use same selection for future launches.

Источник

How to get contact list in android example

Solution 1: Try this too, If you need more reference means refer this link Read ContactList Solution 2: Get contacts info , photo contacts , photo uri and convert to Class model 1). get Contacts and convert to Model Solution 3: and do not forget Solution 1: I’ve developed a utility class to retrieve device contacts.

Android get all contacts

private void getContactList() < ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if ((cur != null ? cur.getCount() : 0) >0) < while (cur != null && cur.moveToNext()) < String cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) >0) < Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[], null); while (pCur.moveToNext()) < String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.i(TAG, "Name: " + name); Log.i(TAG, "Phone Number: " + phoneNo); >pCur.close(); > > > if(cur!=null) < cur.close(); >> 

If you need more reference means refer this link Read ContactList

Get contacts info , photo contacts , photo uri and convert to Class model

 public class ContactModel

2). get Contacts and convert to Model

 public List getContacts(Context ctx) < Listlist = new ArrayList<>(); ContentResolver contentResolver = ctx.getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor.getCount() > 0) < while (cursor.moveToNext()) < String if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) >0) < Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[], null); InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id))); Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)); Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); Bitmap photo = null; if (inputStream != null) < photo = BitmapFactory.decodeStream(inputStream); >while (cursorInfo.moveToNext()) < ContactModel info = new ContactModel(); info.id = id; info.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); info.mobileNumber = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); info.photo = photo; info.photoURI= pURI; list.add(info); >cursorInfo.close(); > > cursor.close(); > return list; > 
public class MyActivity extends Activity implements LoaderManager.LoaderCallbacks < private static final int CONTACTS_LOADER_ID = 1; @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.main); // Prepare the loader. Either re-connect with an existing one, // or start a new one. getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, this); >@Override public Loader onCreateLoader(int id, Bundle args) < // This is called when a new Loader needs to be created. if (id == CONTACTS_LOADER_ID) < return contactsLoader(); >return null; > @Override public void onLoadFinished(Loader loader, Cursor cursor) < //The framework will take care of closing the // old cursor once we return. Listcontacts = contactsFromCursor(cursor); > @Override public void onLoaderReset(Loader loader) < // This is called when the last Cursor provided to onLoadFinished() // above is about to be closed. We need to make sure we are no // longer using it. >private Loader contactsLoader() < Uri contactsUri = ContactsContract.Contacts.CONTENT_URI; // The content URI of the phone contacts String[] projection = < // The columns to return for each row ContactsContract.Contacts.DISPLAY_NAME >; String selection = null; //Selection criteria String[] selectionArgs = <>; //Selection criteria String sortOrder = null; //The sort order for the returned rows return new CursorLoader( getApplicationContext(), contactsUri, projection, selection, selectionArgs, sortOrder); > private List contactsFromCursor(Cursor cursor) < Listcontacts = new ArrayList(); if (cursor.getCount() > 0) < cursor.moveToFirst(); do < String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); contacts.add(name); >while (cursor.moveToNext()); > return contacts; > > 

How to Display Contact List in Android Studio

Contact List Tutorial : In This Video, You Will Learn How to Display Contact List in Android Studio.Build.Gradle :implementation ‘com.google. android .material

How Can I list all mobile contacts on an App Android

I’ve developed a utility class to retrieve device contacts. It’s available on GitHub:

ContactUtils.kt

Since handling all situations that might happen in retrieving contacts is a bit time consuming, I suggest you get this file and add it to your project. It’s written in kotlin , but if you are using java , also it’s possible to get the list of contacts like the following:

List contacts = ContactUtilsKt.retrieveAllContacts(context); // or to retrieve all contacts matching specific search pattern: List contacts = ContactUtilsKt.retrieveAllContacts(context, "John"); 

Here is a function from which you can get contacts

private void getContactList() < ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if ((cur != null ? cur.getCount() : 0) >0) < while (cur != null && cur.moveToNext()) < String cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) >0) < Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[], null); while (pCur.moveToNext()) < String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.i(TAG, "Name: " + name); Log.i(TAG, "Phone Number: " + phoneNo); >pCur.close(); > > > if(cur!=null) < cur.close(); >> 

you must push or set list to your adapter

public List getNumber(ContentResolver cr) < Listphonebook = new ArrayList<>(); Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); // use the cursor to access the contacts while (phones.moveToNext()) < String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); phonebook.add(phoneNumber); >return phonebook; > 

How Can I list all mobile contacts on an App Android, It’s written in kotlin, but if you are using java, also it’s possible to get the list of contacts like the following: List contacts = ContactUtilsKt.retrieveAllContacts (context); // or to retrieve all contacts matching specific search pattern: List contacts = …

Get android contact phone number list

 getNumber(this.getContentResolver()); public void getNumber(ContentResolver cr) < Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); // use the cursor to access the contacts while (phones.moveToNext()) < String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); // get display name phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // get phone number System.out.println(". "+phoneNumber); >> 
 public class MainActivity extends Activity < String phoneNumber; ListView lv; ArrayList aa= new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolver()); >public void getNumber(ContentResolver cr) < Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) < String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println(". "+phoneNumber); aa.add(phoneNumber); >phones.close()// close cursor ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,aa); lv.setAdapter(adapter); //display contact numbers in the list > > 

Make sure you have this in manifest

My solution to recover all contacts:

 Cursor cursor = null; try < cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null); int contactIdIdx = cursor.getColumnIndex(Phone._ID); int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME); int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER); int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID); cursor.moveToFirst(); do < String idContact = cursor.getString(contactIdIdx); String name = cursor.getString(nameIdx); String phoneNumber = cursor.getString(phoneNumberIdx); //. >while (cursor.moveToNext()); > catch (Exception e) < e.printStackTrace(); >finally < if (cursor != null) < cursor.close(); >> 

You need this permission in your manifest :

add the permission in your manifest.

BUT MOST IMPORTANTLY. Didn’t you noticed that just below the line where you formatted the phoneNumber you did not add the names? You should add the name to the number just the way you added the phoneNumber (I enclosed in the code below):

..System.out.println(". "+phoneNumber); aa.add(name); aa.add(phoneNumber); 

the output of the above code will be separate names and numbers on your listview(like this):

name

number

name

in order to display that in one line, you can do this:

and the output will be like this:

How to get phone contacts in Android, The ContentProvider tutorial even uses Contacts as an example: the URI for the table that matches phone numbers to people and the URI for the table that holds pictures of people (both controlled by the Contacts content provider) are: android.provider.Contacts.Phones.CONTENT_URI …

Источник

Оцените статью