我正在开发此联系人应用。到目前为止,我已生成一个ListView女巫有联系人姓名和电话号码。当您点击联系人时,它会启动新活动并显示联系人姓名和电话号码。
我想做的是ListView我只显示了联系人姓名,当你点击列表中的联系人时,活动就开始了,你可以看到姓名和号码。
我想也许我可以隐藏ListView中的一些信息,但我还没有找到任何好处。
那么有人有什么建议吗?
提前致谢。
答案 0 :(得分:3)
首先,仅查询联系人姓名和ID:
在你的清单中你必须声明
<uses-permission android:name="android.permission.READ_CONTACTS" />
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle){
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
// Returns a new CursorLoader
return new CursorLoader(
getActivity(), // Parent activity context
uri, // Table to query
projection, // Projection to return
null, // No selection clause
null, // No selection arguments
sortOrder // Sort by name
);
}
获得带有联系人的光标后,您必须在CursorAdapter
private final static String[] FROM_COLUMNS = {
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY :
Contacts.DISPLAY_NAME
};
private final static int[] TO_IDS = {
android.R.id.text1
};
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
// Gets the ListView from the View list of the parent activity
mContactsList =
(ListView) getActivity().findViewById(R.layout.contact_list_view);
// Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contact_list_item,
null,
FROM_COLUMNS, TO_IDS,
0);
// Sets the adapter for the ListView
mContactsList.setAdapter(mCursorAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}