Why is my contacts query giving a contact with a different id?Intent problem, return value (Android)Why is the Android emulator so slow? How can we speed up the Android emulator?What is the difference between “px”, “dip”, “dp” and “sp”?What is the difference between gravity and layout_gravity in Android?What is the difference between match_parent and fill_parent?Getting Data from ContactsContract.ContactsFetch Contacts in android applicationListView example duplicates contacts in result viewLoadmanager onLoadFinished not calledsetText on button from another activity android
A steel cutting sword?
Where have Brexit voters gone?
Simple fuzz pedal using breadboard
What are the real benefits of using Salesforce DX?
I think I may have violated academic integrity last year - what should I do?
Are there any well known academic philosophy forums?
Could a 19.25mm revolver actually exist?
What to do when you've set the wrong ISO for your film?
Should one buy new hardware after a system compromise?
Where's this lookout in Nova Scotia?
Why does Mjolnir fall down in Age of Ultron but not in Endgame?
How to use " shadow " in pstricks?
Plot twist where the antagonist wins
Would Brexit have gone ahead by now if Gina Miller had not forced the Government to involve Parliament?
In general, would I need to season a meat when making a sauce?
Popcorn is the only acceptable snack to consume while watching a movie
How to illustrate the Mean Value theorem?
How strong are Wi-Fi signals?
Are these reasonable traits for someone with autism?
How to Pin Point Large File eating space in Fedora 18
Looking for a soft substance that doesn't dissolve underwater
What does the view outside my ship traveling at light speed look like?
Website returning plaintext password
keyval - function for keyB should act dependent on value of keyA - how to do this?
Why is my contacts query giving a contact with a different id?
Intent problem, return value (Android)Why is the Android emulator so slow? How can we speed up the Android emulator?What is the difference between “px”, “dip”, “dp” and “sp”?What is the difference between gravity and layout_gravity in Android?What is the difference between match_parent and fill_parent?Getting Data from ContactsContract.ContactsFetch Contacts in android applicationListView example duplicates contacts in result viewLoadmanager onLoadFinished not calledsetText on button from another activity android
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an Activity to allow the user to select contacts. Upon finish, the id of selected contacts is passed in the reply Intent. Where the result is processed, the details of selected contacts are to be read.
The problem is that when processing the result the details read are for a different contact than was selected.
I don't know why. In my case the user is only selecting one contact.
I've read through a lot of documentation and other questions regarding reading contacts, but not seen anything that helps my case.
From the Activity to get the user selection from contacts:
@Override
protected void onCreate (Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_from_contacts);
m_view = findViewById(R.id.lv_contactsSelect);
m_view.setVisibility(View.VISIBLE);
getListView().setItemsCanFocus(false);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
cursor,
new String[]ContactsContract.Contacts.DISPLAY_NAME,
new int[]android.R.id.text1,
0));
Button btn = findViewById(R.id.btn_get_contacts);
btn.setOnClickListener((View view) ->
Intent replyIntent = new Intent();
ArrayList<Long> ids = pickContacts();
replyIntent.putExtra(Activities.ARG_SELECTED, ids);
setResult(Activities.RESULT_CONTACTS_SELECTED, replyIntent);
finish();
);
/** Viewer for list of contacts */
private ListView m_view;
private ListView getListView ()
return m_view;
private CursorAdapter mAdapter;
private void setListAdapter (@NonNull CursorAdapter adapter)
mAdapter = adapter;
m_view.setAdapter(adapter);
// return id for each selected contact
private ArrayList<Long> pickContacts ()
SparseBooleanArray a = getListView().getCheckedItemPositions();
ArrayList<Long> contacts = new ArrayList<>();
for (int i = 0; i < a.size(); i++)
if (a.valueAt(i))
Cursor c = (Cursor)mAdapter.getItem(a.keyAt(i));
// TODO use RawContacts or Contacts? Currently the result is the same.
//Long idContact = c.getLong(c.getColumnIndex(ContactsContract.Contacts._ID));
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
contacts.add(idRaw);
return contacts;
Processing the result:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
if (resultCode == Activities.RESULT_CONTACTS_SELECTED)
ArrayList<Long> ids = (ArrayList<Long>)data.getSerializableExtra(Activities.ARG_SELECTED);
for (long id : ids)
getContactDetails(id);
/**
* As mentioned in https://developer.android.com/reference/android/provider/ContactsContract.RawContacts
* the best way to read a raw contact along with associated data is by using the Entity directory.
*/
// FIXME: The id from the received result matches what was selected,
// but this function reads details for a different contact.
private void getContactDetails (long rawContactId)
System.out.println("Get contact with id " + rawContactId);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
// For example, this output can be "Get contact from entity uri content://com.android.contacts/raw_contacts/615/entity"
// (Where 615 is the id for the selected contact.)
System.out.println("Get contact from entity uri " + entityUri);
Cursor c = getContentResolver().query(entityUri,
new String[]RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1, // projection
null, null, null);
if (c == null)
return;
try
while (c.moveToNext())
// In this example I'm just dumping data to the console.
if (!c.isNull(1))
String mimeType = c.getString(2);
String data = c.getString(3);
System.out.println("mimeType = " + mimeType);
System.out.println("data = " + data);
finally
c.close();
For example, the console output from the handler includes:
mimeType = vnd.android.cursor.item/name
data = A name
Where the name is not the same one as selected in the contact selection activity.
|
show 1 more comment
I have an Activity to allow the user to select contacts. Upon finish, the id of selected contacts is passed in the reply Intent. Where the result is processed, the details of selected contacts are to be read.
The problem is that when processing the result the details read are for a different contact than was selected.
I don't know why. In my case the user is only selecting one contact.
I've read through a lot of documentation and other questions regarding reading contacts, but not seen anything that helps my case.
From the Activity to get the user selection from contacts:
@Override
protected void onCreate (Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_from_contacts);
m_view = findViewById(R.id.lv_contactsSelect);
m_view.setVisibility(View.VISIBLE);
getListView().setItemsCanFocus(false);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
cursor,
new String[]ContactsContract.Contacts.DISPLAY_NAME,
new int[]android.R.id.text1,
0));
Button btn = findViewById(R.id.btn_get_contacts);
btn.setOnClickListener((View view) ->
Intent replyIntent = new Intent();
ArrayList<Long> ids = pickContacts();
replyIntent.putExtra(Activities.ARG_SELECTED, ids);
setResult(Activities.RESULT_CONTACTS_SELECTED, replyIntent);
finish();
);
/** Viewer for list of contacts */
private ListView m_view;
private ListView getListView ()
return m_view;
private CursorAdapter mAdapter;
private void setListAdapter (@NonNull CursorAdapter adapter)
mAdapter = adapter;
m_view.setAdapter(adapter);
// return id for each selected contact
private ArrayList<Long> pickContacts ()
SparseBooleanArray a = getListView().getCheckedItemPositions();
ArrayList<Long> contacts = new ArrayList<>();
for (int i = 0; i < a.size(); i++)
if (a.valueAt(i))
Cursor c = (Cursor)mAdapter.getItem(a.keyAt(i));
// TODO use RawContacts or Contacts? Currently the result is the same.
//Long idContact = c.getLong(c.getColumnIndex(ContactsContract.Contacts._ID));
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
contacts.add(idRaw);
return contacts;
Processing the result:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
if (resultCode == Activities.RESULT_CONTACTS_SELECTED)
ArrayList<Long> ids = (ArrayList<Long>)data.getSerializableExtra(Activities.ARG_SELECTED);
for (long id : ids)
getContactDetails(id);
/**
* As mentioned in https://developer.android.com/reference/android/provider/ContactsContract.RawContacts
* the best way to read a raw contact along with associated data is by using the Entity directory.
*/
// FIXME: The id from the received result matches what was selected,
// but this function reads details for a different contact.
private void getContactDetails (long rawContactId)
System.out.println("Get contact with id " + rawContactId);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
// For example, this output can be "Get contact from entity uri content://com.android.contacts/raw_contacts/615/entity"
// (Where 615 is the id for the selected contact.)
System.out.println("Get contact from entity uri " + entityUri);
Cursor c = getContentResolver().query(entityUri,
new String[]RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1, // projection
null, null, null);
if (c == null)
return;
try
while (c.moveToNext())
// In this example I'm just dumping data to the console.
if (!c.isNull(1))
String mimeType = c.getString(2);
String data = c.getString(3);
System.out.println("mimeType = " + mimeType);
System.out.println("data = " + data);
finally
c.close();
For example, the console output from the handler includes:
mimeType = vnd.android.cursor.item/name
data = A name
Where the name is not the same one as selected in the contact selection activity.
Yes: import android.provider.ContactsContract.RawContacts.Entity;
– ozzylee
Mar 24 at 6:44
The idContact variable is not used. It was something left from trying different id methods. I will remove it to avoid confusion. I don't see how the raw contact id is wrong.
– ozzylee
Mar 24 at 7:45
becauseContactsContract.RawContacts._IDis exactly the same asContactsContract.Contacts._ID- just try toLog.dboth of them
– pskink
Mar 24 at 7:48
Are you saying this line is wrong?Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
– ozzylee
Mar 24 at 7:54
yes, it gives you contact id - not raw contact id - i am assumingmAdapterisSimpleCursorAdapteabove - but i may be wrong sincemAdapteris shown only in one place in your code, btw if you useSimpleCursorAdapterwhat do you need that iteration for?
– pskink
Mar 24 at 7:55
|
show 1 more comment
I have an Activity to allow the user to select contacts. Upon finish, the id of selected contacts is passed in the reply Intent. Where the result is processed, the details of selected contacts are to be read.
The problem is that when processing the result the details read are for a different contact than was selected.
I don't know why. In my case the user is only selecting one contact.
I've read through a lot of documentation and other questions regarding reading contacts, but not seen anything that helps my case.
From the Activity to get the user selection from contacts:
@Override
protected void onCreate (Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_from_contacts);
m_view = findViewById(R.id.lv_contactsSelect);
m_view.setVisibility(View.VISIBLE);
getListView().setItemsCanFocus(false);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
cursor,
new String[]ContactsContract.Contacts.DISPLAY_NAME,
new int[]android.R.id.text1,
0));
Button btn = findViewById(R.id.btn_get_contacts);
btn.setOnClickListener((View view) ->
Intent replyIntent = new Intent();
ArrayList<Long> ids = pickContacts();
replyIntent.putExtra(Activities.ARG_SELECTED, ids);
setResult(Activities.RESULT_CONTACTS_SELECTED, replyIntent);
finish();
);
/** Viewer for list of contacts */
private ListView m_view;
private ListView getListView ()
return m_view;
private CursorAdapter mAdapter;
private void setListAdapter (@NonNull CursorAdapter adapter)
mAdapter = adapter;
m_view.setAdapter(adapter);
// return id for each selected contact
private ArrayList<Long> pickContacts ()
SparseBooleanArray a = getListView().getCheckedItemPositions();
ArrayList<Long> contacts = new ArrayList<>();
for (int i = 0; i < a.size(); i++)
if (a.valueAt(i))
Cursor c = (Cursor)mAdapter.getItem(a.keyAt(i));
// TODO use RawContacts or Contacts? Currently the result is the same.
//Long idContact = c.getLong(c.getColumnIndex(ContactsContract.Contacts._ID));
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
contacts.add(idRaw);
return contacts;
Processing the result:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
if (resultCode == Activities.RESULT_CONTACTS_SELECTED)
ArrayList<Long> ids = (ArrayList<Long>)data.getSerializableExtra(Activities.ARG_SELECTED);
for (long id : ids)
getContactDetails(id);
/**
* As mentioned in https://developer.android.com/reference/android/provider/ContactsContract.RawContacts
* the best way to read a raw contact along with associated data is by using the Entity directory.
*/
// FIXME: The id from the received result matches what was selected,
// but this function reads details for a different contact.
private void getContactDetails (long rawContactId)
System.out.println("Get contact with id " + rawContactId);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
// For example, this output can be "Get contact from entity uri content://com.android.contacts/raw_contacts/615/entity"
// (Where 615 is the id for the selected contact.)
System.out.println("Get contact from entity uri " + entityUri);
Cursor c = getContentResolver().query(entityUri,
new String[]RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1, // projection
null, null, null);
if (c == null)
return;
try
while (c.moveToNext())
// In this example I'm just dumping data to the console.
if (!c.isNull(1))
String mimeType = c.getString(2);
String data = c.getString(3);
System.out.println("mimeType = " + mimeType);
System.out.println("data = " + data);
finally
c.close();
For example, the console output from the handler includes:
mimeType = vnd.android.cursor.item/name
data = A name
Where the name is not the same one as selected in the contact selection activity.
I have an Activity to allow the user to select contacts. Upon finish, the id of selected contacts is passed in the reply Intent. Where the result is processed, the details of selected contacts are to be read.
The problem is that when processing the result the details read are for a different contact than was selected.
I don't know why. In my case the user is only selecting one contact.
I've read through a lot of documentation and other questions regarding reading contacts, but not seen anything that helps my case.
From the Activity to get the user selection from contacts:
@Override
protected void onCreate (Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_from_contacts);
m_view = findViewById(R.id.lv_contactsSelect);
m_view.setVisibility(View.VISIBLE);
getListView().setItemsCanFocus(false);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
cursor,
new String[]ContactsContract.Contacts.DISPLAY_NAME,
new int[]android.R.id.text1,
0));
Button btn = findViewById(R.id.btn_get_contacts);
btn.setOnClickListener((View view) ->
Intent replyIntent = new Intent();
ArrayList<Long> ids = pickContacts();
replyIntent.putExtra(Activities.ARG_SELECTED, ids);
setResult(Activities.RESULT_CONTACTS_SELECTED, replyIntent);
finish();
);
/** Viewer for list of contacts */
private ListView m_view;
private ListView getListView ()
return m_view;
private CursorAdapter mAdapter;
private void setListAdapter (@NonNull CursorAdapter adapter)
mAdapter = adapter;
m_view.setAdapter(adapter);
// return id for each selected contact
private ArrayList<Long> pickContacts ()
SparseBooleanArray a = getListView().getCheckedItemPositions();
ArrayList<Long> contacts = new ArrayList<>();
for (int i = 0; i < a.size(); i++)
if (a.valueAt(i))
Cursor c = (Cursor)mAdapter.getItem(a.keyAt(i));
// TODO use RawContacts or Contacts? Currently the result is the same.
//Long idContact = c.getLong(c.getColumnIndex(ContactsContract.Contacts._ID));
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
contacts.add(idRaw);
return contacts;
Processing the result:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
if (resultCode == Activities.RESULT_CONTACTS_SELECTED)
ArrayList<Long> ids = (ArrayList<Long>)data.getSerializableExtra(Activities.ARG_SELECTED);
for (long id : ids)
getContactDetails(id);
/**
* As mentioned in https://developer.android.com/reference/android/provider/ContactsContract.RawContacts
* the best way to read a raw contact along with associated data is by using the Entity directory.
*/
// FIXME: The id from the received result matches what was selected,
// but this function reads details for a different contact.
private void getContactDetails (long rawContactId)
System.out.println("Get contact with id " + rawContactId);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
// For example, this output can be "Get contact from entity uri content://com.android.contacts/raw_contacts/615/entity"
// (Where 615 is the id for the selected contact.)
System.out.println("Get contact from entity uri " + entityUri);
Cursor c = getContentResolver().query(entityUri,
new String[]RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1, // projection
null, null, null);
if (c == null)
return;
try
while (c.moveToNext())
// In this example I'm just dumping data to the console.
if (!c.isNull(1))
String mimeType = c.getString(2);
String data = c.getString(3);
System.out.println("mimeType = " + mimeType);
System.out.println("data = " + data);
finally
c.close();
For example, the console output from the handler includes:
mimeType = vnd.android.cursor.item/name
data = A name
Where the name is not the same one as selected in the contact selection activity.
edited Mar 24 at 9:19
Fantômas
33.1k156492
33.1k156492
asked Mar 24 at 5:26
ozzyleeozzylee
5611
5611
Yes: import android.provider.ContactsContract.RawContacts.Entity;
– ozzylee
Mar 24 at 6:44
The idContact variable is not used. It was something left from trying different id methods. I will remove it to avoid confusion. I don't see how the raw contact id is wrong.
– ozzylee
Mar 24 at 7:45
becauseContactsContract.RawContacts._IDis exactly the same asContactsContract.Contacts._ID- just try toLog.dboth of them
– pskink
Mar 24 at 7:48
Are you saying this line is wrong?Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
– ozzylee
Mar 24 at 7:54
yes, it gives you contact id - not raw contact id - i am assumingmAdapterisSimpleCursorAdapteabove - but i may be wrong sincemAdapteris shown only in one place in your code, btw if you useSimpleCursorAdapterwhat do you need that iteration for?
– pskink
Mar 24 at 7:55
|
show 1 more comment
Yes: import android.provider.ContactsContract.RawContacts.Entity;
– ozzylee
Mar 24 at 6:44
The idContact variable is not used. It was something left from trying different id methods. I will remove it to avoid confusion. I don't see how the raw contact id is wrong.
– ozzylee
Mar 24 at 7:45
becauseContactsContract.RawContacts._IDis exactly the same asContactsContract.Contacts._ID- just try toLog.dboth of them
– pskink
Mar 24 at 7:48
Are you saying this line is wrong?Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));
– ozzylee
Mar 24 at 7:54
yes, it gives you contact id - not raw contact id - i am assumingmAdapterisSimpleCursorAdapteabove - but i may be wrong sincemAdapteris shown only in one place in your code, btw if you useSimpleCursorAdapterwhat do you need that iteration for?
– pskink
Mar 24 at 7:55
Yes: import android.provider.ContactsContract.RawContacts.Entity;
– ozzylee
Mar 24 at 6:44
Yes: import android.provider.ContactsContract.RawContacts.Entity;
– ozzylee
Mar 24 at 6:44
The idContact variable is not used. It was something left from trying different id methods. I will remove it to avoid confusion. I don't see how the raw contact id is wrong.
– ozzylee
Mar 24 at 7:45
The idContact variable is not used. It was something left from trying different id methods. I will remove it to avoid confusion. I don't see how the raw contact id is wrong.
– ozzylee
Mar 24 at 7:45
because
ContactsContract.RawContacts._ID is exactly the same as ContactsContract.Contacts._ID - just try to Log.d both of them– pskink
Mar 24 at 7:48
because
ContactsContract.RawContacts._ID is exactly the same as ContactsContract.Contacts._ID - just try to Log.d both of them– pskink
Mar 24 at 7:48
Are you saying this line is wrong?
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));– ozzylee
Mar 24 at 7:54
Are you saying this line is wrong?
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));– ozzylee
Mar 24 at 7:54
yes, it gives you contact id - not raw contact id - i am assuming
mAdapter is SimpleCursorAdapte above - but i may be wrong since mAdapter is shown only in one place in your code, btw if you use SimpleCursorAdapter what do you need that iteration for?– pskink
Mar 24 at 7:55
yes, it gives you contact id - not raw contact id - i am assuming
mAdapter is SimpleCursorAdapte above - but i may be wrong since mAdapter is shown only in one place in your code, btw if you use SimpleCursorAdapter what do you need that iteration for?– pskink
Mar 24 at 7:55
|
show 1 more comment
1 Answer
1
active
oldest
votes
in method pickContacts, change it to get the Contacts._ID, it finds an ID by chance, because both RawContacts._ID and Contacts._ID are both the string "_id", but it's just plain wrong.
Next, since you're actually grabbing a contact-id, you need to modify your getContactDetails to accept a ContactId and not a RawContactId.
Not sure why you need to involve Entity APIs like that, if you only need to query for that contact's data, do this:
private void getContactDetails (long contactId)
Log.i("Contacts", "Get contact with id " + contactId);
String[] projection = new String[]Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1;
String selection = Data.CONTACT_ID + "=" + contactId;
Cursor c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
if (c == null)
return;
try
while (c.moveToNext())
String name = c.getString(0);
String mimeType = c.getString(1);
String data = c.getString(2);
Log.i("Contacts", contactId + ", " + name + ", " + mimetype + ", " + data);
finally
c.close();
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320982%2fwhy-is-my-contacts-query-giving-a-contact-with-a-different-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
in method pickContacts, change it to get the Contacts._ID, it finds an ID by chance, because both RawContacts._ID and Contacts._ID are both the string "_id", but it's just plain wrong.
Next, since you're actually grabbing a contact-id, you need to modify your getContactDetails to accept a ContactId and not a RawContactId.
Not sure why you need to involve Entity APIs like that, if you only need to query for that contact's data, do this:
private void getContactDetails (long contactId)
Log.i("Contacts", "Get contact with id " + contactId);
String[] projection = new String[]Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1;
String selection = Data.CONTACT_ID + "=" + contactId;
Cursor c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
if (c == null)
return;
try
while (c.moveToNext())
String name = c.getString(0);
String mimeType = c.getString(1);
String data = c.getString(2);
Log.i("Contacts", contactId + ", " + name + ", " + mimetype + ", " + data);
finally
c.close();
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
add a comment |
in method pickContacts, change it to get the Contacts._ID, it finds an ID by chance, because both RawContacts._ID and Contacts._ID are both the string "_id", but it's just plain wrong.
Next, since you're actually grabbing a contact-id, you need to modify your getContactDetails to accept a ContactId and not a RawContactId.
Not sure why you need to involve Entity APIs like that, if you only need to query for that contact's data, do this:
private void getContactDetails (long contactId)
Log.i("Contacts", "Get contact with id " + contactId);
String[] projection = new String[]Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1;
String selection = Data.CONTACT_ID + "=" + contactId;
Cursor c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
if (c == null)
return;
try
while (c.moveToNext())
String name = c.getString(0);
String mimeType = c.getString(1);
String data = c.getString(2);
Log.i("Contacts", contactId + ", " + name + ", " + mimetype + ", " + data);
finally
c.close();
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
add a comment |
in method pickContacts, change it to get the Contacts._ID, it finds an ID by chance, because both RawContacts._ID and Contacts._ID are both the string "_id", but it's just plain wrong.
Next, since you're actually grabbing a contact-id, you need to modify your getContactDetails to accept a ContactId and not a RawContactId.
Not sure why you need to involve Entity APIs like that, if you only need to query for that contact's data, do this:
private void getContactDetails (long contactId)
Log.i("Contacts", "Get contact with id " + contactId);
String[] projection = new String[]Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1;
String selection = Data.CONTACT_ID + "=" + contactId;
Cursor c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
if (c == null)
return;
try
while (c.moveToNext())
String name = c.getString(0);
String mimeType = c.getString(1);
String data = c.getString(2);
Log.i("Contacts", contactId + ", " + name + ", " + mimetype + ", " + data);
finally
c.close();
in method pickContacts, change it to get the Contacts._ID, it finds an ID by chance, because both RawContacts._ID and Contacts._ID are both the string "_id", but it's just plain wrong.
Next, since you're actually grabbing a contact-id, you need to modify your getContactDetails to accept a ContactId and not a RawContactId.
Not sure why you need to involve Entity APIs like that, if you only need to query for that contact's data, do this:
private void getContactDetails (long contactId)
Log.i("Contacts", "Get contact with id " + contactId);
String[] projection = new String[]Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1;
String selection = Data.CONTACT_ID + "=" + contactId;
Cursor c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
if (c == null)
return;
try
while (c.moveToNext())
String name = c.getString(0);
String mimeType = c.getString(1);
String data = c.getString(2);
Log.i("Contacts", contactId + ", " + name + ", " + mimetype + ", " + data);
finally
c.close();
answered Mar 25 at 7:17
marmormarmor
19.7k886131
19.7k886131
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
add a comment |
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
Thank you. That is a solution without using the entity that I thought was best as, mentioned in developer.android.com/reference/android/provider/…. Anyway, I'm happy to use the query you've given.
– ozzylee
Mar 27 at 10:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320982%2fwhy-is-my-contacts-query-giving-a-contact-with-a-different-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Yes: import android.provider.ContactsContract.RawContacts.Entity;
– ozzylee
Mar 24 at 6:44
The idContact variable is not used. It was something left from trying different id methods. I will remove it to avoid confusion. I don't see how the raw contact id is wrong.
– ozzylee
Mar 24 at 7:45
because
ContactsContract.RawContacts._IDis exactly the same asContactsContract.Contacts._ID- just try toLog.dboth of them– pskink
Mar 24 at 7:48
Are you saying this line is wrong?
Long idRaw = c.getLong(c.getColumnIndex(ContactsContract.RawContacts._ID));– ozzylee
Mar 24 at 7:54
yes, it gives you contact id - not raw contact id - i am assuming
mAdapterisSimpleCursorAdapteabove - but i may be wrong sincemAdapteris shown only in one place in your code, btw if you useSimpleCursorAdapterwhat do you need that iteration for?– pskink
Mar 24 at 7:55