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 6892641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:33:39+00:00 2026-05-27T06:33:39+00:00

just a few questions that I thought I could use some light on. So

  • 0

just a few questions that I thought I could use some light on. So far I have an application that queries the Android Contacts database for certain types of data(currently DISPLAY_NAME, NUMBER, and ADDRESS). Then I want to populate a ListView with the results of my cursor queries. My problem is, I’ve used 3 separate cursors to fetch the data, and I want to bind all 3 sets to entries that I have in my ListView row(R.id.contact_name, R.id.contact_number, R.id.contact_address).

I’m unsure on how to do this with 3 different cursors, and after looking around for the past couple of days I couldn’t find any real guidance except for a few phrases thrown around like using “MergeCursor?”(sp) or trying to join the database tables together. Also, I don’t want to populate my list inside onCreate(). I want to do it inside an AsyncTask’s doInBackground() since some users may have many, many contacts and I don’t want to block the UI thread..which is already happening. But when I tried to implement the population in an Async, Eclipse gives me tons of errors about my SimpleCursorAdapter. Here is my current code. Any guidance on how to improve this architecture and solve the issues I’m having would be greatly appreciate, thanks!

      public class Contacts extends ListActivity {

     private static final String TAG = "Contacts";

     Cursor c, pCur, addrCur;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Uri contacts = ContactsContract.Contacts.CONTENT_URI;
    ContentResolver cr = getContentResolver();

    String[] projection = new String[] { ContactsContract.Data.DISPLAY_NAME };
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";
    Log.d(TAG, "Getting Display Names....");

    // Query the Contacts Content Provider for ONLY Contacts that have phone
    // numbers
    // listed

    c = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER, null,
            sortOrder);
    // Get their display names
    if (c.getCount() > 0) {
        // While there is more data
        while (c.moveToNext()
                && Integer
                        .parseInt(c.getString(c
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            String id = c.getString(c
                    .getColumnIndex(ContactsContract.Contacts._ID));

            String name = c
                    .getString(c
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Query Phone Numbers Next

            /*
             * if (Integer .parseInt(c.getString(c
             * .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
             * > 0) { Log.d(TAG, "Getting Phone Numbers"); pCur = cr.query(
             * ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
             * ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" ,
             * new String[]{id}, null); while (pCur.moveToNext()) {
             * 
             * String phone_number = pCur .getString(pCur
             * .getColumnIndex(ContactsContract
             * .CommonDataKinds.Phone.NUMBER)); } pCur.close(); Log.d(TAG,
             * "Finished Getting Phone Numbers...."); }
             */

            // Query the Addresses
            Log.d(TAG, "Getting Addresses....");
            String addrWhere = ContactsContract.Data.CONTACT_ID
                    + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";

            String[] addWhereParams = new String[] {
                    id,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };

            addrCur = cr.query(ContactsContract.Data.CONTENT_URI, null,
                    null, null, null);

            while (addrCur.moveToNext()) {
                String poBox = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));

                String street = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));

                String city = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));

                String state = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));

                String postalCode = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));

                String country = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));

                String type = addrCur
                        .getString(addrCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

            }
            addrCur.close();
            Log.d(TAG, "Finished Getting Addresses....");

        }
    }
    // new loadContacts().execute(TAG);
    final int[] TO = { R.id.contact_name };
    final String[] FROM = new String[] { ContactsContract.Contacts.DISPLAY_NAME };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.contact_item, c, FROM, TO);
    setListAdapter(adapter);
    // adapter.changeCursor(pCur);


}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
     c.moveToPosition(position);

     String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

     Toast.makeText(this, "You Selected  " + name, Toast.LENGTH_SHORT).show();





    //Toast t = Toast.makeText(Contacts.this, "You Selected  " + position, Toast.LENGTH_SHORT);
    //t.show();
}

}

  • 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-05-27T06:33:39+00:00Added an answer on May 27, 2026 at 6:33 am

    MergeCursor is more for concatonating the results of one query with the results of another. Think of it as more rows, not more columns.

    If you’re pulling from multiple sources because you want columns from each query (phone number from one, address from another), what you want to use is a CursorJoiner. A CursorJoiner will let you take two cursors sorted by the same primary key, and create a new Cursor with the columns you want from each parent cursor. A brief how-to overview is included in the linked documentation.

    Good luck!

    EDIT: To answer the bit about running the query on a separate thread, a good place to start would be to read up on Loaders.

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

Sidebar

Related Questions

I'm quite new to develop on GAE. Need just sort out few questions about
I added a function in my .vimrc that's just few search and replace commands.
i've just read a few posts on hiding Silverlight code in some way. Main
I've posted a few questions prior to this one regarding the use of std::function
I have done some research on this question and most answers are a few
I have some existing (largish) ASP.NET web applications that people are starting to visit
I have read a few similar questions and answers, but none completely address my
I know there are plenty of questions about 3d rotation that have been answered
Writing my first Android app. Thought I would start with a Corporate Directory application.
I want to make a simple 2d terrain with just a few bumps and

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.