1

This activity is working but i want to save contacts information (phone_number , name , contact_id) to a ArrayList . Then i will use a customadapter to listview this arraylist in checkbox for multiselecting construction . I will use contact informations in the later stages of the program. Help me with your ideas please. Thanks .

MainActivity.java

package com.javacodegeeks.android.androidphonecontactsexample;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;

public class MainActivity extends Activity {
    public TextView outputText;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        outputText = (TextView) findViewById(R.id.textView1);
        fetchContacts();
    }

    public void fetchContacts() {

        String phoneNumber = null;
        String email = null;
        //http://yazilimdevi.com/yazilimdevi/Makaleler-867-hashtable-vs--hashmap.aspx
        //http://stackoverflow.com/questions/19974166/contacts-adding-in-arraylist-for-listview-error




        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        Uri EmailCONTENT_URI =  ContactsContract.CommonDataKinds.Email.CONTENT_URI;
        String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
        String DATA = ContactsContract.CommonDataKinds.Email.DATA;


        StringBuffer output = new StringBuffer();

        ContentResolver contentResolver = getContentResolver();

        Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);  


        // Loop for every contact in the phone
        if (cursor.getCount() > 0) {



            while (cursor.moveToNext()) {



                String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
                String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));

                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));

                if (hasPhoneNumber > 0) {

                    output.append("\n First Name:" + name);


                    // Query and loop for every phone number of the contact
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);

                    while (phoneCursor.moveToNext()) {
                        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                        output.append("\n Phone number:" + phoneNumber);



                    }

                    phoneCursor.close();

                    // Query and loop for every email of the contact
                    Cursor emailCursor = contentResolver.query(EmailCONTENT_URI,    null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null);

                    while (emailCursor.moveToNext()) {

                        email = emailCursor.getString(emailCursor.getColumnIndex(DATA));

                        output.append("\nEmail:" + email);


                    }

                    emailCursor.close();
                }

                output.append("\n");
            }

            outputText.setText(output);
        }
    }

}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:textSize="20sp"
        android:gravity="center"
        android:text="@string/ContactsInformation" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_below="@+id/textView"
        android:gravity="center"
        android:text="@string/TextView" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidPhoneContactsExample</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="ContactsInformation">Contacts Information</string>
    <string name="TextView">TextView</string>
</resources>
  • Requesting links to off-site resources is considered off-topic for Stack Overflow. What did you try, and what specific problems did you encounter? – CommonsWare May 11 '14 at 23:30
  • Sorry . I tried something but i delete them because there was many errors . I will ask again after that i find my old problems in my codes. But i asked this firstly maybe somebody can from scratch show the way.Sorry for my english :/ Also i look to this link , it is similiar with mine but couldn't find a usefull answer in there : http://stackoverflow.com/questions/12470363/how-to-add-android-contacts-into-a-listview-with-a-check-box –  May 12 '14 at 00:21
  • i edited my question . Now , i need to use Arraylist to keep contacts . –  May 20 '14 at 13:06

1 Answers1

2

add an arraylist at top

ArrayList<String> arr = new ArrayList<String>();

then change your while loop as follow

 while (cursor.moveToNext()) {

            output = new StringBuffer();  //re initialize your StringBuffer again

            String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
            String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));

            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));

            if (hasPhoneNumber > 0) {

                output.append("\n First Name:" + name);


                // Query and loop for every phone number of the contact
                Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);

                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append("\n Phone number:" + phoneNumber);



                }

                phoneCursor.close();

                // Query and loop for every email of the contact
                Cursor emailCursor = contentResolver.query(EmailCONTENT_URI,    null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null);

                while (emailCursor.moveToNext()) {

                    email = emailCursor.getString(emailCursor.getColumnIndex(DATA));

                    output.append("\nEmail:" + email);


                }

                emailCursor.close();
            }

            output.append("\n");

            arr.add(output.toString());  //add contact here in your array list.
        }

now arr will contain all contacts.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • Thanks a lot ! Also i will ask one question to. What is the this ArrayList struction's difference with " ArrayList contact = ArrayList(); " ?? I tried to use "ArrayList contact = ArrayList();" but couldn't achieve :/ –  May 20 '14 at 13:18
  • According to your array list that u created. its mean you need a custom object Contacts which mean you need to define a class which contain three fields . name , phone , email . you assign valeus to them and then you will store in contact array list..:). – Waqar Ahmed May 20 '14 at 16:30
  • It seems this solution is taking too much time. I have almost 900 contacts, and it's taking almost 10 seconds to fetch the whole set of contacts. – AKA Nov 02 '16 at 09:57