Android ContactsContract with AutocompleteTextview too slow - android

Am trying to use the ContactsProvider with my AutoCompleteTextview using a method that fetches the data (name and phone number) and stores them in a list. As expected, this method will always take time to complete as am calling the method in the onCreateView method of my Fragment class.
This is the method:
...
ArrayList<String> phoneValues;
ArrayList<String> nameValues;
...
private void readContactData() {
try {
/*********** Reading Contacts Name And Number **********/
String phoneNumber = "";
ContentResolver contentResolver = getActivity()
.getContentResolver();
//Query to get contact name
Cursor cursor = contentResolver
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
// If data data found in contacts
if (cursor.getCount() > 0) {
int k=0;
String name = "";
while (cursor.moveToNext())
{
String id = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Check contact have phone number
if (Integer
.parseInt(cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
//Create query to get phone number by contact id
Cursor pCur = contentResolver
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { id },
null);
int j=0;
while (pCur
.moveToNext())
{
// Sometimes get multiple data
if(j==0)
{
// Get Phone number
phoneNumber =""+pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Add contacts names to adapter
autocompleteAdapter.add(name);
// Add ArrayList names to adapter
phoneValues.add(phoneNumber.toString());
nameValues.add(name.toString());
j++;
k++;
}
} // End while loop
pCur.close();
} // End if
} // End while loop
} // End Cursor value check
cursor.close();
} catch (Exception e) {
Log.i("AutocompleteContacts","Exception : "+ e);
}
}
Am sure there is a better way to accomplish this, but this method works and the suggestions are presented when I type into the AutocompleteTextview. Am just worried about the time it takes. How can I accomplish this without populating an ArrayList?
I have looked at this question: Getting name and email from contact list is very slow and applied the suggestions in the answer to my code, but now nothing is suggested when I type.How can I improve the performance of my current code?

This is how i have implemented AutoCompleteTextView and it works fine for me ..
final AutoCompleteTextView act=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayList<String> alContacts = new ArrayList<String>();
ArrayList<String> alNames= new ArrayList<String>();
ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
alNames.add(contactName);
alContacts.add(contactNumber);
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
String[] array=new String[alNames.size()];
array=(String[]) alNames.toArray(array);
ArrayAdapter<String> myArr= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,array);
act.setAdapter(myArr);
act.setThreshold(1);

I got rid of the slow response by placing the method inside an AsynTask.
public class AutocompleteAsyncTask extends AsyncTask<Void, Void, Void> {
public Void doInBackground(Void...params) {
try {
/*********** Reading Contacts Name And Number **********/
String phoneNumber = "";
ContentResolver contentResolver = getActivity()
.getContentResolver();
//Query to get contact name
Cursor cursor = contentResolver
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
// If data data found in contacts
if (cursor.getCount() > 0) {
int k=0;
String name = "";
while (cursor.moveToNext())
{
//...Rest of the same code as above
and then calling this in my onCreateView():
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
new AutocompleteAsyncTask().execute();
return rootView;
}
Now the task of inflating my view and fetching the data are separated into two different threads.
The CursorLoader option mentioned by Eugen Pechanec is kinda the best option, so I'll update this answer with that option when I can.

Related

Avoid delay in loading an activity when hit on fab in android

When I click on fab to write a new SMS in my application, it is opening too slowly. When I comment readcontactData or adapter it is working Quickly. where is the problem
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,new ArrayList<String>());
readContactData();
contactNumber.setThreshold(1);
//Set adapter to AutoCompleteTextView
contactNumber.setAdapter(adapter);
contactNumber.setOnItemClickListener(this);
private void readContactData() {
try {
ContentResolver contentResolver = getBaseContext().getContentResolver();
// Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
//Query to get contact name
Cursor cursor =contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
// If data found in contacts
if (cursor.getCount() > 0) {
Log.i("AutocompleteContacts", "Reading contacts........");
int k=0;
while (cursor.moveToNext())
{
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//Check contact have phone number
if ((Integer.parseInt(hasPhone) > 0))
{
//Create query to get phone number by contact id
Cursor pCur = contentResolver
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id,
null,
null);
int j=0;
while (pCur.moveToNext())
{
// Sometimes get multiple data
if(j==0)
{
// Get Phone number
String phoneNumber =pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Add contacts names to adapter
adapter.add(name);
//adapter.add(phoneNumber);
// Add ArrayList names to adapter
phoneValueArr.add(phoneNumber.toString());
nameValueArr.add(name.toString());
j++;
//k++;
}
} // End while loop
pCur.close();
} // End if
} // End while loop
//} // End Cursor value check
cursor.close();
}
catch (Exception e)
{
e.printStackTrace();
Log.d("this is an error","akdjfkandkfj");
}
}**
You are loading data in Main thread. So its slow. Use Async Task for readContact.
Writing too much code in onCreate, slow the loading.

What is the best way to get distinct contacts in Android

I am successfully storing contacts in parse.com dashboard data browser by this code.
public void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) ==1) {
System.out.println(name );
ParseObject testObject = new ParseObject("Contacts");
testObject.put("names", name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println( phone);
testObject.put("phonenumber", phone);
}
pCur.close();
testObject.saveInBackground();
}
}
}
}
But there is no check for the duplicate contacts !
It stores all the contacts duplicate from sim / phone memory.
How can it be avoided ?
One possible method I think is to store distinct names(contact) in local database, & then retrieving that data to store it in parse.com
Is there exists a better way ?
Thanks in advance...
An easy approach could be to load the data to a MatrixCursor with no duplicate data. For example lets assume you have a cursor c1 will many contacts, but you need a cursor with no duplicate data. Here is what you could do:
MatrixCursor mc = new MatrixCursor(new String[] {
Phone._ID,
Phone.DISPLAY_NAME_PRIMARY,
Phone.NUMBER
});
String lastNumber = "";
while(c1.moveToNext()){
String id = c1.getString(c1.getColumnIndexOrThrow(Phone._ID));
String name = c1.getString(c1.getColumnIndexOrThrow(Phone.DISPLAY_NAME_PRIMARY)));
String number = c1.getString(c1.getColumnIndexOrThrow(Phone.NUMBER));
//Some condition to check previous data is not matched and only then add row
if(!lastNumber.contains(number)){
lastNumber = number;
mc.addRow(new String[]{id, name, number});
}
}
c1.close();
Make an instance of MatrixCursor with same columns, and then load if last number or contact name does not match that of the previous contact. The condition for checking is upto you. Query data in some order so that the duplicate contacts stay together first.
Once the MatrixCursor is loaded you can fetch data from it. You could also attach it to a view through a custom CursorLoader or CursorAdapter.
Please see the below method. You will get contacts list which does not have duplicate phone numbers.
public void readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ArrayList<ParseObject> contacts = new ArrayList<ParseObject>();
ArrayList<String> list = new ArrayList<String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) == 1) {
System.out.println(name);
ParseObject testObject = new ParseObject("Contacts");
testObject.put("names", name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(phone);
testObject.put("phonenumber", phone);
if(!list.contains(phone)) {
contacts.add(testObject);
}
list.add(phone);
}
pCur.close();
testObject.saveInBackground();
}
}
}
}
Set is a collection in java that does not allow duplicates. You can put your data into a set with number as a key and name as value, to avoid duplicate numbers.
And later you can take them back from set and put into your testObject with name as key and number as value.
Here is the Solution that i worked out for you....
You can go through the logcat for information about how it works 100%
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
public class MainActivity extends Activity {
String ClsSimPhonename = null;
String ClsSimphoneNo = null;
public static ArrayList<String> phonecontact = new ArrayList<String>();
public static ArrayList<String> simcontact = new ArrayList<String>();
public static ArrayList<String> totalcontact = new ArrayList<String>();
public static ArrayList<String> repeatedcontact = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get phone contact...
getphonecontact();
// get sim contact...
getsimcard_contact();
System.out.println("phone??? " + phonecontact);
System.out.println("sim??? " + simcontact);
System.out.println("sim_size??? " + simcontact.size());
System.out.println("phone_size??? " + phonecontact.size());
System.out.println("totalcontact_size??? " + totalcontact.size());
// filter process beigins here....
nowFilterContact();
}
private void nowFilterContact() {
// TODO Auto-generated method stub
// determine which contact have more item....
if (simcontact.size() > phonecontact.size()) {
onemorefiltermethod(simcontact.size(), simcontact, phonecontact);
} else {
onemorefiltermethod(phonecontact.size(), phonecontact, simcontact);
}
}
private void onemorefiltermethod(int size, ArrayList<String> contacts,
ArrayList<String> contact2) {
// TODO Auto-generated method stub
// compare both contact and get repeated contacts....
for (int i = 0; i < size; i++) {
try {
if (contacts.contains(contact2.get(i))) {
// add repeated contacts to array....
repeatedcontact.add(contact2.get(i));
}
} catch (Exception e) {
}
}
System.out.println("repeatedcontact_size??? " + repeatedcontact.size());
// now delete repeated contact from total contact
now_deletedrepeated_contact_from_total();
}
private void now_deletedrepeated_contact_from_total() {
// TODO Auto-generated method stub
for (int i = 0; i < totalcontact.size(); i++) {
try {
if (totalcontact.contains(repeatedcontact.get(i))) {
totalcontact.remove(repeatedcontact.get(i));
}
} catch (Exception e) {
}
}
System.out.println("Final contact size" + totalcontact.size());
System.out.println("Final contact " + totalcontact);
}
private void getsimcard_contact() {
// TODO Auto-generated method stub
try {
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri, null,
null, null, null);
while (cursorSim.moveToNext()) {
ClsSimPhonename = cursorSim.getString(cursorSim
.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim
.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D", "");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename = ClsSimPhonename.replace("|", "");
/*
* add contact from phone to array phone array and total array
*/
phonecontact.add(ClsSimphoneNo);
totalcontact.add(ClsSimphoneNo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void getphonecontact() {
// TODO Auto-generated method stub
try {
String[] PROJECTION = new String[] { Contacts._ID,
Contacts.DISPLAY_NAME, Phone.NUMBER };
Cursor c = managedQuery(Phone.CONTENT_URI, PROJECTION, null, null,
null);
if (c.moveToFirst()) {
String ClsPhonename = null;
String ClsphoneNo = null;
do {
ClsPhonename = c.getString(c
.getColumnIndex(Contacts.DISPLAY_NAME));
ClsphoneNo = c.getString(c.getColumnIndex(Phone.NUMBER));
/*
* add contact from sim to array sim array and total array
*/
simcontact.add(ClsphoneNo);
totalcontact.add(ClsphoneNo);
ClsphoneNo.replaceAll("\\D", "");
ClsPhonename = ClsPhonename.replaceAll("&", "");
ClsPhonename.replace("|", "");
String ClsPhoneName = ClsPhonename.replace("|", "");
} while (c.moveToNext());
}
} catch (Exception e) {
}
}
}
Permission
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Your output is on now_deletedrepeated_contact_from_total() method.
Check totalcontact array value for Final output

Android - cant get phone number of some contacts

I'm having a problem with extracting phone numbers of some people in my contact list.
First I show all the contacts in a listview:
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
mCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] {mContactId}, null);
When clicking on an item, this is how I fetch the contact_id:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Cursor currentCursor = mContactsAdapter.getCursor();
if (currentCursor != null) {
notifyOnContactSelectedListeners(String.valueOf(id));
}
}
Then I create a new fragment, and while loading it I query for the contact's phone & display name:
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
So for some people that has a phone, I get the phone number this way and that's ok.
But for some people I can't get the phone number this way - but they do have phone number in the default's phone contacts book.
What went wrong?
I had a similar difficulty. I discovered that the numbers that I was unable to receive had all been imported from my linked Facebook account. You will be able to detect that the contact exists, and indeed that they have a phone number. However, when you try to retrieve said number with a SQL query the result returned will be null.
It transpired that Facebook restrict access to their contacts for security reasons. I am yet to find another provider (e.g. LinkedIn, Google) which hides phone numbers.
Further reading: Cannot find Facebook contacts in RawContacts
try this may it useful for you
public class Contact extends Activity implements OnItemClickListener{
private static final int PICK_CONTACT = 0;
Cursor c;
Cursor cursor,phones,emails,address;
String id,phoneNo,name;
String[] from;
int[] to;
ListView lv;
Cursor cur,pCur;
List<String> list1 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
lv = (ListView)findViewById(R.id.contactlist);
displayContacts();
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list1));
lv.setOnItemClickListener(this);
}
private void displayContacts() {
ContentResolver cr = getContentResolver();
cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// setContact(name,phoneNo);
System.out.println("name"+name+"ph no"+phoneNo);
list1.add(name+"\n"+phoneNo);
// Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String s = lv.getItemAtPosition(arg2).toString();
Log.i("my msg", s.substring(0, s.indexOf("\n")));
Toast.makeText(this, s.substring(s.indexOf("\n")+1,s.length() ),1 ).show();
}
}
I received null in some contacts.
I verified my code to find out that when querying phone numbers I was using ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER then I changed to ContactsContract.CommonDataKinds.Phone.NUMBER that according to android docs it says.
The phone number as the user entered it.
and the code worked well.

Get all contacts from android mobile to android application in android 4.0?

I m making one app in which I want to have all records from my android mobile 4.0 to my android application. I have done this also. but problem is I have almost 200 contacts in my phonebook but I m getting only 90 records randomly in my application. I have tried a lot. but nothing solution I found out. can any one has solution? below is my code :
ContentResolver cr=getContentResolver();
Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cur.getString
(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER ))) > 0)
{
//Query phone here. Covered next
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new
String[]{id}, null);
while (pCur.moveToNext())
{
// Do something with phones
String pnumber
=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactids.add(id);
names.add(name.trim().toLowerCase());
PhoneNumbers.add(pnumber);
}
pCur.close();
}
else
{
String pnumber="";
contactids.add(id);
names.add(name);
PhoneNumbers.add(pnumber);
}
}
finally I have done with following code. But the problem with this is it can't fetch records if contacts are more then 1500. and till 1500 records process is very slow.
public class MyActivity extends Activity {
public Cursor cur;
public int j=0;
#Override
public void onCreate(Bundle savedInstanceState)
{
cur=getContacts();
startManagingCursor(cur);
cur.moveToFirst();
Button btn=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
contactids=new ArrayList<String>();
names=new ArrayList<String>();
PhoneNumbers=new ArrayList<String>();
try
{
new showdialog1(MyActivity.this).execute();
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
});
}
class showdialog1 extends AsyncTask<Void, Void, Void>
{
public showdialog1(Activity act) {
super.onPreExecute();
}
#Override
protected void onPreExecute() {
dialog = ProgressDialog.show(MyActivity.this,"title", "message");
}
#Override
protected Void doInBackground(Void... params) {
if (cur.getCount() > 0)
{
do
{
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"=" +id, null, null);
if(pCur.getCount()>0)
{
while (pCur.moveToNext())
{
// Do something with phones
String pnumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactids.add(id);
names.add(name.trim().toLowerCase());
PhoneNumbers.add(pnumber);
}
pCur.close();
}
else
{
String pnumber="";
contactids.add(id);
names.add(name.trim().toLowerCase());
PhoneNumbers.add(pnumber);
}
}while (cur.moveToNext());
cur.close();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
dialog.dismiss();
int contactidsize=contactids.size();
int namesize=contactids.size();
int numbersize=contactids.size();
saverecords();
return;
}
}
}
you get your contact list of your phone..
try this code and after that if you want to use contact from list you select that contace and use details of that contact (ex: number, name ,email.id etc..)
plz goto below link
/*** USE this CODE ******/
int PICK_CONTACT=1;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);`
You are using the condition:
Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER ))) > 0)
which does not consider the contacts that doesnot have phone numbers .
Hope this works
See ContactManager http://developer.android.com/resources/samples/ContactManager/index.html
Create and initialize boolean variable mShowInvisible - if set to true, it will list all contacts regardless of user preference
/**
* Obtains the contact list for the currently selected account.
*
* #return A cursor for for accessing the contact list.
*/
private Cursor getContacts(){
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}

Get contact details from contact id in Android

I want to get several contact details from a contact list view. I have this code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
//HERE I WANT TO GET CONTACT DETAILS FROM THE ID PARAMETER
Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, Uri.encode(id));
Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME}, null,null,null);
try {
c.moveToFirst();
String displayName = c.getString(0);
} finally {
c.close();
}
}
But I get this exception: IllegalArgumentException, Invalid lookup id (when I call query method from cursor).
So I dont know how to get a valid lookup id from the item list.
Any idea? thanks!
Here id means, A contact id for which you want to fetch the contact details,
The simply code for getting phone number for a particular contact id is like,
// Build the Uri to query to table
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
// Query the table
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
// Get the phone numbers from the contact
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("Phone: " + phoneNumber + "\n");
}
}
So, from your question I have to doubt for id parameter which are you sing in your uri just clear that, Also in my example the id is string type...
Hope you will understand it.
Update: Uri.encode(id) instead of just pass id in string format.
Thanks..
Because managedQuery is deprecated you can also use
Cursor phoneCursor = getContentResolver().query(myPhoneUri, null, null, null, null);
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int columnIndex = arg2.getColumnIndex(ContactsContract.Contacts._ID);
private void retriveContactImage(ImageView imageView, int columnIndex) {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(columnIndex)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(photo);
}
private ArrayList<String> retrieveContactNumbers(TextView textView, long contactId) {
ArrayList<String> phoneNum = new ArrayList<String>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId + "" }, null);
if (cursor.getCount() >= 1) {
while (cursor.moveToNext()) {
// store the numbers in an array
String str = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (str != null && str.trim().length() > 0) {
phoneNum.add(str);
}
}
}
cursor.close();
textView.setText(phoneNum.get(0));
return phoneNum;
}
private void retrieveContactName(TextView textView, long contactId) {
String contactName = null;
Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts.DISPLAY_NAME },
ContactsContract.Contacts._ID + " = ?", new String[] { String.valueOf(contactId) }, null);
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
textView.setText(contactName);
}

Categories

Resources