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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:05:14+00:00 2026-05-17T21:05:14+00:00

I’m rather new to both Java programming and to Android development, so my learning

  • 0

I’m rather new to both Java programming and to Android development, so my learning curve is rather steep at the moment. I seem to be stuck on something that I can’t find decent examples for how to work past it.

I wrote a function that gets all my phone’s contacts based on examples and docs I found here & there online. The problem I cannot seem to work through is this. The following code works just fine;

    private void fillData() {
    // This goes and gets all the contacts
    // TODO: Find a way to filter this only on contacts that have mobile numbers
    cursor = getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null);

    final ArrayList<String> contacts = new ArrayList<String>();

    // Let's set our local variable to a reference to our listview control
    // in the view.
    lvContacts = (ListView) findViewById(R.id.lvContacts);

    while(cursor.moveToNext()) {
        contacts.add(cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)));
    }

    // Make the array adapter for the listview.
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);

    // Let's sort our resulting data alphabetically.
    aa.sort(new Comparator<String>() {
        public int compare(String object1, String object2) {
            return object1.compareTo(object2);
        };
    });

    // Give the list of contacts over to the list view now.
    lvContacts.setAdapter(aa);
}

I want to alter the query statement by filtering out all contacts that do not have a mobile phone number entry.
I attempted something like this;

        cursor = getContentResolver().query(Contacts.CONTENT_URI,
        new String[] {Data._ID, Phone.TYPE, Phone.LABEL},
        null, null, null);

But when I do that it throws a NullPointer exception error. What’s wrong with this?
I got that from an example on android’s site, but they had a where clause that didn’t apply to my needs, so I change the where stuff to null. Is that what’s screwing this up?

Thanks.

  • 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-17T21:05:15+00:00Added an answer on May 17, 2026 at 9:05 pm

    Well, it appears that I have arrived at the solution for my problem on my own. (after having pulled the hair out of my head and becoming fashionably bald)

    It seems that the use of the Phone.TYPE was most definitely the problem, indeed. Phone.TYPE is a constant and not a data column.

    It turns out that the code that worked perfectly was this;

        private void fillData() {
        // This goes and gets all the contacts that have mobile numbers
    
        final ArrayList<String> contacts = new ArrayList<String>();
    
        // Let's set our local variable to a reference to our listview control
        // in the view.
        lvContacts = (ListView) findViewById(R.id.lvContacts);
    
        String[] proj_2    = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
        phnCursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
        while(phnCursor.moveToNext()) {
            if ( phnCursor.getInt(2) == Phone.TYPE_MOBILE ) {
                String name = phnCursor.getString(1);
                contacts.add(name);
            }
        }
    
        // Make the array adapter for the listview.
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
                                      android.R.layout.simple_list_item_multiple_choice,
                                      contacts);
    
        // Let's sort our resulting data alphabetically.
        aa.sort(new Comparator<String>() {
            public int compare(String object1, String object2) {
                return object1.compareTo(object2);
            };
        });
    
        // Give the list of contacts over to the list view now.
        lvContacts.setAdapter(aa);
    }
    

    I appreciate the help, but unfortunately must say that thorough bouts of madness and research eventually paid off. Hopefully this helps someone else out.

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

Sidebar

Related Questions

No related questions found

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.