I was following an example off of the android site. I am new to android development. The problem I am having is that my contacts repeat over and over, about 6 times. Can anyone figure out why? I feel it may have to do with my imports as they were not included with the example but I am not certain. Also note that I did not create an xml file for the listview under res. Thanks,
ListViewLoader.java
package com.example.contactlist;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
//from http://developer.android.com/guide/topics/ui/layout/listview.html
public class ListViewLoader extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data
SimpleCursorAdapter mAdapter;
// These are the Contacts rows that we will retrieve
static final String[] PROJECTION = new String[] {
ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME };
// This is the select criteria
static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
+ " NOTNULL) AND (" + ContactsContract.Data.DISPLAY_NAME
+ " != '' ))";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// For the cursor adapter, specify which columns go into which views
String[] fromColumns = { ContactsContract.Data.DISPLAY_NAME };
int[] toViews = { android.R.id.text1 }; // The TextView in
// simple_list_item_1
// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null, fromColumns,
toViews, 0);
setListAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
// Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
PROJECTION, SELECTION, null, null);
}
// Called when a previously created loader has finished loading
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);
}
// Called when a previously created loader is reset, making the data
// unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
}
}
I've figured out the solution. I needed to query the Contacts table instead of the Data table. Apparently the Data table has duplicates. I've pasted a portion of my updated code.
public class ListViewLoader extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data
SimpleCursorAdapter mAdapter;
// These are the Contacts rows that we will retrieve
static final String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
// This is the select criteria
static final String SELECTION = "((" + ContactsContract.Contacts.DISPLAY_NAME
+ " NOTNULL) AND (" + ContactsContract.Contacts.DISPLAY_NAME
+ " != '' ))";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// For the cursor adapter, specify which columns go into which views
String[] fromColumns = { ContactsContract.Contacts.DISPLAY_NAME };
int[] toViews = { android.R.id.text1 }; // The TextView in
// simple_list_item_1
// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null, fromColumns,
toViews, 0);
setListAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
System.out.println("oncreate (Bundle)");
}
// Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
System.out.println("oncreateloader");
System.out.println(PROJECTION);
System.out.println(SELECTION);
return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI,
PROJECTION, SELECTION, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With