Duplicate contacts in resultant List View using cursor adapter

756 Views Asked by At

I am creating simple contact application where i want to display only name and number in the list view. I used cursor adapter to load the contact name and number alone in the list of columns(FROM string array) in Simple Cursor Adapter. However my output lists out contact email id and duplicate names. Here is my code. Please help me to figure out where i went wrong to extract name and number alone.

  import android.app.ListActivity;
  import android.app.LoaderManager.LoaderCallbacks;
  import android.content.CursorLoader;
  import android.content.Intent;
  import android.content.Loader;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.provider.ContactsContract;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.ListView;
  import android.widget.ProgressBar;
  import android.widget.SimpleCursorAdapter;
  import android.widget.Toast;

public class ListViewLoader extends ListActivity implements
    LoaderCallbacks<Cursor> {

SimpleCursorAdapter mAdapter;

static final String[] PROJECTION = new String[] {
        ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER };

static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
        + " NOTNULL) AND (" + ContactsContract.Data.DISPLAY_NAME
        + " != '' ) AND ("
        + ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER
        + " != 0 ))";;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    progressBar.setIndeterminate(true);
    getListView().setEmptyView(progressBar);

    ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
    root.addView(progressBar);

    String[] fromColumns = { ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };
    int[] toViews = { android.R.id.text1, android.R.id.text2 };

    mAdapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_2, null, fromColumns,
            toViews, 0);
    setListAdapter(mAdapter);

    getLoaderManager().initLoader(0, null, this);
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
            PROJECTION, SELECTION, null, null);
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    mAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader) {

    mAdapter.swapCursor(null);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

}

   }

My Output that lists out email ids which i do not want to get populated:

A [email protected]

B [email protected]

C [email protected]

D [email protected]

A (000)000-000

B (111)111-111

(Since I do not have enough reputation, I am unable to upload my output image. Sorry for that)

0

There are 0 best solutions below