Honeycomb / ICS 中的地址格式
我有一个应用程序,可以从设备上的联系信息中检索邮寄地址。我使用联系人 API 使用类似于下面的代码检索街道地址、邮政编码、城市等信息。然后,我将地址显示在单独的行上,用户可以接受它或根据需要更改它。
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null);
...
while(addrCur.moveToNext()) {
String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
...
}
addrCur.close();
这在 Android 2.2/2.3 上工作得很好,但我注意到在 3.x 及更高版本上,返回的整个地址可以格式化为单个字符串,并在 STREET 字段中换行。当在设备上创建联系人/地址时会发生这种情况;如果我从 Gmail 同步联系人,则会正确检索地址(我怀疑同步的信息保存在正确的字段中)。我看不到任何方法来获取这些类型的地址的各个数据元素(街道、邮政编码等)。这可能吗?我需要使用新的 API 吗?
I have an app which can retrieve the mailing address from contact information on the device. I retrieve the information for street address, post code, city etc. using the contacts API using code similar to below. I then display the address on separate lines and the user can accept it or change it if desired.
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null);
...
while(addrCur.moveToNext()) {
String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
...
}
addrCur.close();
This has worked fine with Android 2.2/2.3 but I noticed on 3.x and higher that the entire address that is returned can be formatted as a single string with line breaks in the STREET field. This occurs when the contact/address is created on the device; if I sync a contact from Gmail, the address is retrieved correctly (I suspect that the synced info is saved in the correct fields). I can't see any way to get the individual data elements (street, postcode, etc) for these types of addresses. Is this possible? Is there a new API I need to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有找到办法做到这一点。我发现的唯一解决方法是通过从光标获取
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
列来获取完整的格式化地址,然后尝试将其分解(假设有几种不同的标准格式)。除了使用自由格式文本框之外,无法在 Honeycomb/ICS 联系人应用程序中输入地址,因此我猜联系人应用程序在将其存储到数据库之前不会尝试自动解析它。希望我是错的,并且有一种方法可以做到这一点 - 但我还没有找到。
I haven't found a way to do it. The only workaround I've found is to get the full, formatted address by getting the
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
column from the cursor and then attempt to break it apart assuming a couple different standard formattings. There's no way to input an address in the Honeycomb/ICS Contacts app other than using a free-form text box, so I guess the Contacts app doesn't try and parse it automatically before storing it in the database.Hopefully I'm wrong and there is a way to do it - but I haven't found it yet.