Domanda

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

È stato utile?

Soluzione

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top