我正在尝试在点击时显示联系人号码,我已经有一个指向正确联系人的光标以及一个联系人uri,我可以获得联系人的DISPLAY_NAME但是当我尝试获取电话号码时出现错误。这是我的ContactListFragment类:
public class ContactListFragment extends ListFragment implements AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private ContactsAdapter mAdapter;
public ContactListFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ContactsAdapter(getActivity());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(mAdapter);
getListView().setOnItemClickListener(this);
getLoaderManager().initLoader(ContactsQuery.QUERY_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id == ContactsQuery.QUERY_ID)
{
Uri contentUri = ContactsQuery.CONTENT_URI;
return new CursorLoader(getActivity(),
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
null,
ContactsQuery.SORT_ORDER);
}
return null;
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final Cursor cursor = mAdapter.getCursor();
cursor.moveToPosition(position);
final Uri uri = Contacts.getLookupUri(cursor.getLong(ContactsQuery.ID), cursor.getString(ContactsQuery.LOOKUP_KEY));
// How do I get the phone number here?
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (loader.getId() == ContactsQuery.QUERY_ID) {
mAdapter.swapCursor(data);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
if (loader.getId() == ContactsQuery.QUERY_ID) {
mAdapter.swapCursor(null);
}
}
public interface ContactsQuery {
final static int QUERY_ID = 1;
final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
@SuppressLint("InlinedApi")
final static String SELECTION = (Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME) + "<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1";
@SuppressLint("InlinedApi")
final static String SORT_ORDER = Utils.hasHoneycomb() ? ContactsContract.Contacts.SORT_KEY_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;
@SuppressLint("InlinedApi")
final static String[] PROJECTION = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY, Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME,
SORT_ORDER,
};
// The query column numbers which map to each value in the projection
final static int ID = 0;
final static int LOOKUP_KEY = 1;
final static int DISPLAY_NAME = 2;
final static int SORT_KEY = 3;
}
这是我收到的错误(我的清单上已经有READ_CONTACTS权限):
03-09 13:40:36.675 25055-25055/com.example.android.launcher E/CursorWindow﹕ Failed to read row 1, column -1 from a CursorWindow which has 352 rows, 5 columns.
03-09 13:40:36.675 25055-25055/com.example.android.launcher D/AndroidRuntime﹕ Shutting down VM
03-09 13:40:36.675 25055-25055/com.example.android.launcher W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415e4ba8)
03-09 13:40:36.675 25055-25055/com.example.android.launcher E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.launcher, PID: 25055
java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
如何在onItemClick中获取电话号码?
答案 0 :(得分:1)
您需要一个额外的查询,如下所示:
if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) == 1) {
String id = String.valueOf(cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
Cursor phoneCursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[] {id}, null);
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String normalizedPhoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
Log.v("myapp", "phone # - " + phoneNumber);
Log.v("myapp", "normalized phone # - " + normalizedPhoneNumber);
}
}