如何使用游标加载器获取联系人的一些信息?

时间:2013-08-05 08:01:06

标签: android android-cursorloader

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

我想我可能错了:PROJECTION&选择(?) 你的意见怎么样?你能告诉我我的错误是什么吗?

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

1 个答案:

答案 0 :(得分:1)

尝试更改ContactsContract.Contacts._ID

的每个ContactsContract.CommonDataKinds.Phone._ID 对于ContactsContract.Contacts.DISPLAY_NAME

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME