chkeff Posted January 7, 2011 Share Posted January 7, 2011 Bonjour à tous ! Voila mon problème: pour une application j'ai besoin de parcourir la liste des contacts afin de mettre un nom à un numéro de téléphone. Etant un peu novice avec le SDK Android je me suis inspiré d'un code trouvé sur internet que j'ai modifié. Voici le code: private String RecupererContact(String num) { // instance qui permet d'acceder au contenu d'autre application ContentResolver ConnectApp = this.getContentResolver(); Uri uri = Contacts.People.CONTENT_URI; String[] projection = new String[] {People.NAME, People.NUMBER}; // on récupere les contacts dans un curseur Cursor cur = ConnectApp.query(uri, projection, null, null, null); this.startManagingCursor(cur); if (cur.moveToFirst()) { do { String numero = cur.getString(cur.getColumnIndex(People.NUMBER)); Log.i("", numero); if(numero == num){ return cur.getString(cur.getColumnIndex(People.NAME)); } } while (cur.moveToNext()); } return null; } Il s'agit donc d'une fonction à laquelle j'envoie en paramètre un numéro de téléphone. Le problème c'est que la variable "numero" contient toujours Null au lieu d'un numéro. Je travaille avec l'émulateur dans lequel je n'ai qu'un seul contact. En faisant des Log sur les données du cursor, je retrouve bien un contact avec don nom mais pas moyen d'avoir son numéro ! De plus j'utilise des fonctions Dépréciées mais avec le "ContactsContract" je ne m'en sort pas ! Si quelqu'un pouvait m'aider je le remercie d'avance ! Link to comment Share on other sites More sharing options...
chkeff Posted January 10, 2011 Author Share Posted January 10, 2011 Bon je donne des nouvelles, j'ai essayer de comprendre un peu les nouvelles fonctions et j'ai finit par plus ou moins comprendre en m'inspirant d'un code trouver sur internet. Voici donc ma fonction aprés modification: private String RecupererContact(String num) { 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) { Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); while (pCur.moveToNext()) { //bloquer ici } pCur.close(); } } } return null; } Je bloque donc à la ligne commentée, je sais que dans cette boucle je parcours les numéros du contact mais je ne sais pas comment les récupérer ! Et mon Cursor pCur contient 54 colonnes donc jsais pas trop quoi faire ! J'ai juste besoin de ça pour terminer cette fonction ^^. Quelqu'un peut m'aider ? Merci d'avance ! Link to comment Share on other sites More sharing options...
chkeff Posted January 18, 2011 Author Share Posted January 18, 2011 C'est bon j'ai résolu mon problème. Pour les intérréssés voici comment j'ai fait: private String RecupererContact(String num) { String contact = null; String numfr = num.replace("+33", "0"); ContentResolver cr = getContentResolver(); Cursor curNumero = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER +" = ? OR " + ContactsContract.CommonDataKinds.Phone.NUMBER +" = ?", new String[]{num, numfr}, null); if(curNumero.moveToFirst()){ //si on [color=red]as[/color] trouvé un numéro String id = curNumero.getString(curNumero.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));; Cursor curContact = cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{id}, null); if(curContact.moveToFirst()){ contact = curContact.getString(curContact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); curContact.close(); } } curNumero.close(); return contact; } Si vous avez des questions n'hésitez pas =) Link to comment Share on other sites More sharing options...
bigstorm Posted January 19, 2011 Share Posted January 19, 2011 C'est vrai qu'il y a certaines choses qui changent dans l'accès du répertoire! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.