質問

I'm trying to display contact number on click, I already have a cursor pointing to the right contact as well as a contact uri and I can get the contact's DISPLAY_NAME however when I try to get the phone number I get an error. This is my ContactListFragment class:

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

This is the error I was getting (I already have READ_CONTACTS permission on my manifest):

 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.

How can I get the phone number inside onItemClick?

役に立ちましたか?

解決

You need an additional query, like so:

    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);
        }
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top