Android的getContentResolver()查询错误 [英] Android getContentResolver().query error

查看:684
本文介绍了Android的getContentResolver()查询错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的这两个Java程序和Android开发,所以我的学习曲线相当陡峭的时刻。我似乎被卡住的东西,我无法找到如何过去,工作体面的例子。

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.

我写了我的获取手机的基于实例和文档我发现这里和功放的所有联系人功能;网上有。我似乎无法工作,通过问题是这样的。下面code工作就好了;

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);

但是,当我这样做,它抛出一个空指针异常错误。这有什么错呢?
我是从Android的网站的例子,但他们认为并不适用于我的需求where子句,所以我改变那里的东西为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?

感谢。

推荐答案

哦,看来我在我自己对我的问题的解决方案已经抵达。 (后经拉头发,我的头,并成为时髦秃)

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)

看来,使用Phone.TYPE的是最绝的问题,确实如此。 Phone.TYPE是一个常数,而不是一个数据列

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.

原来的code,它完美地工作了这一点;

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);
}

我AP preciate的帮助,但不幸的是必须说,疯狂和研究的深入较量,最终得到了回报。希望这可以帮助其他人了。

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

这篇关于Android的getContentResolver()查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆