1

I want to get: id, name, phone number and company of all contacts which stored in default contact of the phone. After that, I want to display them into list view. I use cursor loader to do that. BUT, i have just get id and name of each contact. I CAN NOT get phone number & company. You can see all my code below.

I think I may be wrong at: PROJECTION & SELECTION (?) What about your opinion?? Could you show me what my error is?

public class MainActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>{
final Context context = this;
protected Intent intent;
protected TextView contactId;
protected ListView lv;
protected EditText inputSearch;
protected SimpleAdapter adapter;
SimpleCursorAdapter curAdapter;
public MatrixCursor extras;

    SimpleCursorAdapter mAdapter;

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

    static final String SELECTION = "("+ 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + ContactsContract.Contacts._ID + " AND " + 
            ContactsContract.Data.CONTACT_ID + " = " + ContactsContract.Contacts._ID + " AND " + 
            ContactsContract.Data.MIMETYPE + " = " + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE + 
            ")";


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

    String[] fromColumns = {ContactsContract.Contacts._ID,
                            ContactsContract.Contacts.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER,
                            ContactsContract.CommonDataKinds.Organization.DATA};

    int[] toViews = {       R.id.contactId,
                            R.id.contactName,
                            R.id.phone,
                            R.id.company};

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

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

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI,
                PROJECTION, SELECTION, null, "DISPLAY_NAME ASC");           
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}

}

OK. And here is all information in logcat: https://lh5.googleusercontent.com/-rXZ0iSq_CEg/Uf9bk-CmwBI/AAAAAAAAAVQ/SvieJesBqqQ/s800/Untitled.png

R700
  • 129
  • 2
  • 9

1 Answers1

1

Try changing each ContactsContract.Contacts._ID for ContactsContract.CommonDataKinds.Phone._ID

and ContactsContract.Contacts.DISPLAY_NAME for ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

Uniruddh
  • 4,427
  • 3
  • 52
  • 86
Renan Bandeira
  • 3,238
  • 17
  • 27
  • Great idea, Renan Bandeira! I try to do it and get id, name, phone number successfully. BUT, what about company name (?) – R700 Aug 05 '13 at 08:20
  • Sorry, forgot this one. Here it is: ContactsContract.CommonDataKinds.Organization.COMPANY – Renan Bandeira Aug 05 '13 at 08:33
  • I have get value of ContactsContract.CommonDataKinds.Organization.COMPANY of each contact. They are PHONE NUMBER (?) Values of Organization.COMPANY & Phone.DISPLAY_NAME are the same (!) what's wrong? – R700 Aug 05 '13 at 08:51
  • My bad. I had never tried to get Organization info and just thought it would be ok. Look at this code, it will help you. Or just copy and paste the method that works here. http://stackoverflow.com/a/9723988/2652124 – Renan Bandeira Aug 05 '13 at 08:54