我想得到:存储在手机默认联系人中的所有联系人的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