views:

75

answers:

2

Hey folks,

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.

A: 

try this tutorial...

It covers the phone number query you are looking to code up

Aaron Saunders
I checked out that tutorial and will bookmarked it, but it is overlooking the 'best practices' aspect of developing on the Android. Android recommends that you select only the columns that you wish to you rather then all columns as that tutorial clearly is doing. They say that providing a projection is more efficient. So, I may be able to use some of the ideas there, but need a better scope of using the query method with projections and where clauses for efficiency purposes. Hence the second code snippet I provided. But thank you for the link though. :)
Skittles
A: 

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.

Skittles