Reputation: 21
I am developing this contacts app. So far i have generated a ListView witch has contact name and phone number. When you click on contact it starts new activity and shows contact name and phone number.
What I wanna do is that the ListView I have shows only contact names and when you click on contact in the list then the activity starts and you can see both name and number.
I thought maybe I can hide some of the information in ListView but I haven´t found anything good on that.
So does anybody have any suggestions?
Thanks in advance.
Upvotes: 1
Views: 60
Reputation: 109
First of all, query only contact name and id:
In your manifest you have to declare
<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
);
}
Once you got the Cursor with contacts you have to pass that in a 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);
}
}
Upvotes: 3