2

I am posting my code here. Please help me to get the email address of the contact. Also I am getting a random list; please help me to sort the data as well.

package com.example.webwerks.retrievingcontactsdemo;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;


public class MyActivity extends Activity {
  private String name;
  private String Number;
  private String alternateNumber;
  private String email_id;
  RelativeLayout relativeLayout;
  ListView listView;
  ArrayList <String> arrayList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    listView = (ListView)findViewById(android.R.id.list);
    getInformation(this.getContentResolver());
 //   relativeLayout = (RelativeLayout)findViewById(R.id.layoutRelative);


}
public void getInformation(ContentResolver contentResolver){
    Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
    while (phones.moveToNext()){
        name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        Number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        email_id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        arrayList.add(name);
        arrayList.add(Number);
        arrayList.add(email_id);
    }
    phones.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(adapter);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

This is my code. It gives me the contact name and contact number(primary) and email id column. Also, it shows me the contact number only. And the data is not sorted according to my phone contact list. Please help. Thanks in advance :)

rgettman
  • 176,041
  • 30
  • 275
  • 357
frost
  • 520
  • 1
  • 3
  • 13

2 Answers2

2

You need to execute another query using the id to get Email details of tat contact. SO check this code

Better to go with DataModel class:

public class ContactData
{
    public String name;
    public String email;
}


ArrayList<ContactData> data = new ArrayList<>(); 
  public void getInformation(ContentResolver contentResolver){
            Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);


        while (phones.moveToNext()){
            String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = contentResolver.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                            new String[]{id}, null); 
            while (cur1.moveToNext()) { 
                //to get the contact names
                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e("Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email", email);


                if(email!=null){
                    Toast.makeText(this,
                            "Name : "+ name+" EMail  : "+email, 1000)
                            .show();
                }
            } 
            dataList.add(contactData);
            cur1.close();


        }
        phones.close();




    }
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • I tried this code. I am getting the email address of the person when i debuged it. But that email address were not the one's corresponding to that contact :( if you can guide me proper as i m newbie – frost Sep 30 '14 at 05:54
  • And also i am not getting the entire contact list. And the list is not sorted also – frost Sep 30 '14 at 05:58
  • are you sure it's not corresponding ?? As I am gettong perfect..Go with [this](http://stackoverflow.com/a/10117750/2591002) and tell me your output – SweetWisher ツ Sep 30 '14 at 06:03
  • Yes I Tried it but thats not giving me proper answer. Its gives me random email address for random contact. – frost Sep 30 '14 at 06:29
  • follow [this](http://androidexample.com/Get_Contact_Emails_By_Content_Provider_-_Android_Example/index.php?view=article_discription&aid=121&aaid=141) buddy – SweetWisher ツ Sep 30 '14 at 06:32
  • I'm getting correct email for correct person..might be something is wrong with your code :( – SweetWisher ツ Sep 30 '14 at 06:37
  • Can you please put the edited code that i can just paste in my project. Because I have done all things the same but then too. As in Number also which is missing from the above code. – frost Sep 30 '14 at 06:56
  • I have just toasted the email..so putting that code...then accordingly add it to Array as you need to show it in list.. Check ma code now – SweetWisher ツ Sep 30 '14 at 06:59
  • Yes It did Worked. It gave me email address number and name, But now the problem is in the list view the data is random eg: I got Name of some person email of some other person and number of some other person. The toast was giving me the exact result which i wanted but the listvew certainly doesnt do it for me – frost Sep 30 '14 at 07:13
  • Then correctly add it to list Buddy :) Create a class having number name and email adderss and add the data the the arraylist of that class – SweetWisher ツ Sep 30 '14 at 07:17
  • Yes will try Thanks a lot for the help :) Can you just vote my question up such that it can add up in the reputation – frost Sep 30 '14 at 07:20
  • I have edited the code to use Model class :) glad it helped you .. :) – SweetWisher ツ Sep 30 '14 at 07:24
  • Is this ArrayList in which class? The Mainactivity or the new class that is created? – frost Sep 30 '14 at 09:06
  • whatever you want to do .... You can write it in Mainactivity also or can create new class. Better to go with new class – SweetWisher ツ Sep 30 '14 at 09:09
  • But why does this code gives me limited contacts? I have some 500 Contacts on my device but it gives something around 70-80 contacts. Why it can be so? – frost Sep 30 '14 at 09:13
  • Might be your 70-80 contacts only are having Email id – SweetWisher ツ Sep 30 '14 at 09:15
  • ohk So I Dont want like that. I needed all the contacts details which are there on my device. example: if a contact has a name and number then it should be displayed in a list if one has number name and email then it should be done accordingly – frost Sep 30 '14 at 09:17
  • then `dataList.add(contactData);` must be in outer loop... change your code accordingly.First check the size of `cur1` – SweetWisher ツ Sep 30 '14 at 09:23
  • One more doubt I want edit and update the details from the application and the changes should also reflect in the original contact list on the device. How can I achieve that? – frost Oct 01 '14 at 03:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62220/discussion-between-sweetwisher--and-chinmay-ghag). – SweetWisher ツ Oct 01 '14 at 04:22
0
 cursor = context.getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
 cursor.moveToFirst();

 for (int i = 0; i < cursor.getCount(); i++) {

   contact = getContact(cursor, context.getContentResolver());
   cursor.moveToNext();
  }

  private Contact getContact(Cursor cur , ContentResolver cr){
    String contactID = cur.getString(cur
            .getColumnIndex(ContactsContract.Contacts._ID));
  if (contactID != null) 
 {

     // for reading all contact
        Cursor pCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[]{contactID}, null);
        while (pCur.moveToNext()) {

            String contactNumber = pCur
                    .getString(pCur
                              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contact_Number_Type = pCur
                    .getString(pCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

            contact.setContact_Number(contactNumber);
            contact.setContact_Number_Type(contact_Number_Type);

        }
        pCur.close();

        // for reading all email id

        Cursor emailCur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{contactID}, null);
        while (emailCur.moveToNext()) {

            String emailContact = emailCur
                    .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            String emailType = emailCur
                    .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

            contact.setContact_Email(emailContact);
            contact.setContact_Email_Type(emailType);
        }

        emailCur.close();
}

}

Here Contact is a bean class