1

我想得到:存储在手机默认联系人中的所有联系人的id、姓名、电话号码和公司。之后,我想将它们显示到列表视图中。我使用游标加载器来做到这一点。但是,我刚刚得到每个联系人的 ID 和姓名。我无法获得电话号码和公司。你可以在下面看到我的所有代码。

我想我可能错在:投影和选择(?)你的意见呢?你能告诉我我的错误是什么吗?

public class MainActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>{
final Context context = this;
protected Intent intent;
protected TextView contactId;
protected ListView lv;
protected EditText inputSearch;
protected SimpleAdapter adapter;
SimpleCursorAdapter curAdapter;
public MatrixCursor extras;

    SimpleCursorAdapter mAdapter;

    static final String[] PROJECTION = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Organization.DATA};

    static final String SELECTION = "("+ 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + ContactsContract.Contacts._ID + " AND " + 
            ContactsContract.Data.CONTACT_ID + " = " + ContactsContract.Contacts._ID + " AND " + 
            ContactsContract.Data.MIMETYPE + " = " + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE + 
            ")";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] fromColumns = {ContactsContract.Contacts._ID,
                            ContactsContract.Contacts.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER,
                            ContactsContract.CommonDataKinds.Organization.DATA};

    int[] toViews = {       R.id.contactId,
                            R.id.contactName,
                            R.id.phone,
                            R.id.company};

    mAdapter = new SimpleCursorAdapter(this, 
            R.layout.view_contact_entry, null,
            fromColumns, toViews, 0);

    setListAdapter(mAdapter);
    getLoaderManager().initLoader(0, null, this);
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI,
                PROJECTION, SELECTION, null, "DISPLAY_NAME ASC");           
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}

}

好的。以下是 logcat 中的所有信息: https ://lh5.googleusercontent.com/-rXZ0iSq_CEg/Uf9bk-CmwBI/AAAAAAAAAVQ/SvieJesBqqQ/s800/Untitled.png

4

1 回答 1

1

尝试更改ContactsContract.Contacts._ID每个ContactsContract.CommonDataKinds.Phone._ID

并且ContactsContract.Contacts.DISPLAY_NAME对于ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

于 2013-08-05T08:06:19.293 回答