开发者

Android getContentResolver().query error

开发者 https://www.devze.com 2023-01-21 23:15 出处:网络
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 w

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.开发者_StackOverflow社区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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号