Skip to content

Instantly share code, notes, and snippets.

@vicky7230
Last active April 30, 2016 08:34
Show Gist options
  • Save vicky7230/9852b18e43e1a64076f859941bc30809 to your computer and use it in GitHub Desktop.
Save vicky7230/9852b18e43e1a64076f859941bc30809 to your computer and use it in GitHub Desktop.
code to get contact name ,contact number and display them in a listview in android.
package com.cool.vicky.contentprovider;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private SimpleCursorAdapter adapter;
public static final int CONTACT_LOADER_ID = 78;
// Defines the asynchronous callback for the contacts data loader
private LoaderManager.LoaderCallbacks<Cursor> contactsLoader =
new LoaderManager.LoaderCallbacks<Cursor>() {
// Create and return the actual cursor loader for the contacts data
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Define the columns to retrieve
String[] projectionFields = new String[]{ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
// Construct the loader
CursorLoader cursorLoader = new CursorLoader(MainActivity.this,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, // URI
projectionFields, // projection fields
null, // the selection criteria
null, // the selection args
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC" // the sort order
);
// Return the loader for use
return cursorLoader;
}
// When the system finishes retrieving the Cursor through the CursorLoader,
// a call to the onLoadFinished() method takes place.
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// The swapCursor() method assigns the new Cursor to the adapter
adapter.swapCursor(cursor);
}
// This method is triggered when the loader is being reset
// and the loader data is no longer available. Called if the data
// in the provider changes and the Cursor becomes stale.
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// Clear the Cursor we were using with another call to the swapCursor()
adapter.swapCursor(null);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupCursorAdapter();
// Initialize the loader with a special ID and the defined callbacks from above
getSupportLoaderManager().initLoader(CONTACT_LOADER_ID,
new Bundle(), contactsLoader);
ListView lvContacts = (ListView) findViewById(R.id.lvContacts);
lvContacts.setAdapter(adapter);
}
// Create simple cursor adapter to connect the cursor dataset we load with a ListView
private void setupCursorAdapter() {
// Column data from cursor to bind views from
String[] uiBindFrom = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
// View IDs which will have the respective column data inserted
int[] uiBindTo = {R.id.tvName, R.id.ivNumber};
// Create the simple cursor adapter to use for our list
// specifying the template to inflate (item_contact),
adapter = new SimpleCursorAdapter(
this, R.layout.item_contact,
null, uiBindFrom, uiBindTo,
0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment