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

  • SEARCH
  • Home
  • 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 8108115
In Process

The Archive Base Latest Questions

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

I am using the Android Eclair 2.1 platform. The working behind this code is,

  • 0

I am using the Android Eclair 2.1 platform.

The working behind this code is, it will access all the contacts from the Emulator and will display in the list view for each contact like this

Contact Name, Contact Number, Email

There are 2 issues for this code is

  1. I did run the program, after that I created a new contact it won’t be in alphabetical order

[ eg : when I create a name starting from B it wouldn’t go after A, instead it go to the last place ]

2.If a contact has no Email or Number it will receive the previous contact’s Email or Number

Here is the 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_PH    ONE_NUMBER))) > 0)
{
    Cursor pCur =cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDat     aKinds.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();
 }
cursor.close(); 
 }
 }

Any link or site to refer and study to solve this problems if so send me the link ?

  • 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:59:41+00:00Added an answer on June 6, 2026 at 12:59 am

    Check the tested code below

    package stack.examples;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Email;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    
    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.listView_items);  
           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();
                }
                    contactlist.add(displayName+","+phoneNumber+","+ emailAddress);
    
            }
            cursor.close(); 
            sortList(contactlist);
            itemAdapter.notifyDataSetChanged();
        }
    
        private static void sortList(List<String> aItems){
            Collections.sort(aItems, String.CASE_INSENSITIVE_ORDER);
          }
    }
    

    To sort the ArrayList in Alphabetical order, you can use

    Collections.sort(aItems, String.CASE_INSENSITIVE_ORDER);
    

    Happy Coding !!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using android 2.1 platform.The code I have will display the Name number
I am using Android 2.1 platform, I have 7 contacts in my emulator when
I'm using Android 2.3, so according to this post: Streaming Audio from A URL
I am using android:uiOptions=splitActionBarWhenNarrow to split the Menu's .it is working fine with emulator
Using Android opengl I need to move an object from point A to point
I currently using android NDK to write some native code in C. I have
I am working on sending Meeting Invites from my Android app. Here is the
Building an app using android.support.v4.app , targeting API level 8. Everything's working as planned
I'm using android 3.0 and I try to create bitmap xml that will create
Using android sdk 2.3. How can I synchronously update the UI from a service.

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.