【问题标题】:Android getContentResolver().query errorAndroid getContentResolver().query 错误
【发布时间】:2010-10-27 10:03:55
【问题描述】:

我对 Java 编程和 Android 开发都很陌生,所以目前我的学习曲线相当陡峭。我似乎被困在了一些我找不到合适的例子来说明如何克服它的事情上。

我编写了一个函数,该函数根据我在网上和网上找到的示例和文档获取我所有手机的联系人。我似乎无法解决的问题是这个。以下代码可以正常工作;

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

我想通过过滤掉所有没有手机号码条目的联系人来更改查询语句。 我尝试过这样的事情;

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

但是当我这样做时,它会引发 NullPointer 异常错误。这有什么问题? 我是从 android 网站上的一个示例中得到的,但他们有一个 where 子句不适用于我的需求,所以我将 where 的内容更改为 null。这就是搞砸的原因吗?

谢谢。

【问题讨论】:

  • 问题似乎是它不喜欢新 String[] {Data._ID, Phone.TYPE, Phone.LABEL} 参数中的“data2”列引用。 Phone.TYPE 转换为“data2”,错误消息说明 SQLiteQueryBuilder.computeProjection 对该列不太满意。不知道如何解决这个问题。

标签: android contacts


【解决方案1】:

好吧,看来我已经自己解决了我的问题。 (在把头发从我头上拔下来并成为时髦的秃头之后)

看来,Phone.TYPE 的使用确实是问题所在。 Phone.TYPE 是一个常量,而不是数据列。

原来完美运行的代码是这样的;

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

我感谢您的帮助,但不幸的是,必须说,彻底的疯狂和研究最终得到了回报。希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多