Android getContentResolver().查询错误
我对 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。这就是搞砸的原因吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,看来我已经自己找到了解决问题的方法。 (在把头发从我的头上拔下来并变成时髦的秃头之后)
看来 Phone.TYPE 的使用确实是最明显的问题。 Phone.TYPE 是一个常量,而不是数据列。
事实证明,完美运行的代码是这样的;
我很感激你的帮助,但不幸的是,必须说,彻底的疯狂和研究最终得到了回报。希望这可以帮助其他人。
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;
I appreciate the help, but unfortunately must say that thorough bouts of madness and research eventually paid off. Hopefully this helps someone else out.