Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8105169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:05:41+00:00 2026-06-06T00:05:41+00:00

I am using android 2.1 platform.The code I have will display the Name number

  • 0

I am using android 2.1 platform.The code I have will display the Name number and email ID of all the persons from the Emulator contact list,by using this code I will get the requirement as on above.I have some contacts in my emulator, I created two more contacts (say C and D). BUT the issues are

Issue 1. If I create a new contact having name and number not email, This contact will take the email ID from the contact just down to it.for eg: if I create a name C has no email ID but D has email ID, D is the contact already in the Emulator. C will take D’s email ID.C and D have same email ID.

Issue 2. If again I create a new contact having name email but not number, This contact will take the number from the contact I previously created.for eg: if I create the name E has no number but it will take the number of the contact C. so C and E have the same number .

I am using this code

public class GetAllDatas extends Activity {

ListView lvItem;
private Button btnAdd;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>(); 
ArrayAdapter<String> itemAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lvItem = (ListView)this.findViewById(R.id.lvitems); 
    btnAdd = (Button)this.findViewById(R.id.btnAddItem);

    itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
    lvItem.setAdapter(itemAdapter);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            readContacts();
        }
    });

} 

private void readContacts()
{
    ContentResolver cr =getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) 
    {
        displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));


        Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
        while (emails.moveToNext()) 
        { 
            emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
            break;
        }
        emails.close();


        if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
            while (pCur.moveToNext()) 
            {
                 phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                break;  
            } 
            pCur.close();
        }


        //  To display the Details
        contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n");
        itemAdapter.notifyDataSetChanged();
    }
    Collections.sort(contactlist);
    cursor.close(); 
}
}

How can I solve this problem ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:05:42+00:00Added an answer on June 6, 2026 at 12:05 am

    As i Understand your question..

    Before add new item in reference variables just erase the previous one..
    like,

    emailAddress = "" ; 
    displayName  = "" ;
    phoneNumber  = "" ; 
    

    in your while loop for fetching contact information and adding it to list..

    private void readContacts()
    {
        ContentResolver cr =getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
            emailAddress = "" ;
            displayName  = "" ;
            phoneNumber  = "" ;
    
            displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    
            Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
            while (emails.moveToNext()) 
            { 
                emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
                break;
            }
            emails.close(); 
            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                while (pCur.moveToNext()) 
                {
                     phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    break;  
                } 
                pCur.close();
            }   
    
            //  To display the Details
            contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n");          
        }
        Collections.sort(contactlist);
        itemAdapter.notifyDataSetChanged();
        cursor.close(); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

all I have downloaded source code from google android website following google's guide. My
I am using Android 2.1 platform, I have 7 contacts in my emulator when
How to include jar files of the Android platform when using the javah tool
i'm using aspectJ in my Android project because of monkeytalk ( cross platform testing
I am using the MvvmCross framework to build a cross platform (iOS, Android, WP7)
Using android sdk 2.3. How can I synchronously update the UI from a service.
Using Android opengl I need to move an object from point A to point
We are trying to have an online handwritten text recognizer for the Android platform.
I am trying to upload pictures on the wall of Facebook using Android platform.
I have a few questions regarding android source code download, repo / git. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.