Textview et Contacts - Android2eeAndroid2ee formation expert android
× GUI

Textview et Contacts

More
11 years 10 months ago #31 by remy.silber@free.fr
Bjr
Je n'arrive pas combiner la textview et les contacts.
En partant du code suivant

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
}
people.close();

Réponse

"Vérifier que la valeur contact en utilisant un log comme ci dessous.
Vérifier également les permissions

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
Log.v("TAG", "valeur du string:"+contact);
}
people.close();"


Réponse

Merci pour ton aide,

Je confirme que le Log me donne bien une liste de contacts vraiment très large.... mais ça ressemble à ce que je souhaite.
Si je comprends bien la logique je dois créer un tableau et une ligne de code dans la boucle qui ajoute la variable string dans le tableau ?

Question subsidiaire, y'a-t-il un moyen de visualiser l'ensemble des informations relatives aux contacts, ou afficher le tableau général des contacts ?

Merci

Please Log in or Create an account to join the conversation.

More
11 years 10 months ago - 11 years 10 months ago #32 by mseguy
Replied by mseguy on topic Re: Textview et Contacts
Bonjour,
L'idée est plus objet que ca en fait. D'abord tu définis un Objet MyContact et tu y mets les champs qui t’intéressent (Nom, Prénom, List<Tel>,List<Email>,...). Ensuite tu charges ces MyContact dans une liste que tu vas utiliser dans la suite de ton programme. Ce qui donne un truc comme ça:
private List<MyContact> getContacts(){
List<MyContact>contacts=new ArrayList<MyContact>();
MyContact currentContact;

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
currentContact=new MyContact();
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
currentContact.setName(contact);
... et ainsi de suite avec les champs que tu rencontres...
contacts.add(currentContact);
}
people.close();
return contacts;
}
Pour ce qui est de l'ensemble des informations, saches que tu peux définir une projection (dire dans le query quelles sont les colonnes que tu souhaites rapatrier). Dans le query tu peux mettre une clause Where pour ne récupérer que les contacts qui ont un numéro de téléphone par exemple).
Ensuite pour connaitre la liste des colonnes renvoyées, il y a plein de tutos partout, par exemple stackOverFlow StackOverFlow contact browse :
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}

// Find Email Addresses
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();

Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId, null, null);
while (address.moveToNext()) {
// These are all private class variables, don't forget to create them.
poBox = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
street = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
city = address
.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
state = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
postalCode = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
country = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
type = address
.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
} // address.moveToNext()
} // while (cursor.moveToNext())
cursor.close();

Voilà, normalement avec ca tu devrais t'en sortir.:)

Mathias Séguy
This email address is being protected from spambots. You need JavaScript enabled to view it.
Auteur Android2ee.com
Docteur en Mathématiques Fondamentales
Directeur Technique & Avant-vente
Expert Technique de l'Agence Nationale de la Recherche
Rédacteur sur Developpez.com
Last edit: 11 years 10 months ago by mseguy.

Please Log in or Create an account to join the conversation.

More
11 years 10 months ago #33 by remy.silber@free.fr
Je vais étudier ca, merci

Please Log in or Create an account to join the conversation.

More
11 years 9 months ago #35 by remy.silber@free.fr
Slt
Je continue à chercher une solution ... je ne suis que développeur du dimanche ;)

Avec le code ci-dessous, j'arrive bien à afficher les nom de mes contacts dans la listView

package list.view.dyna;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListwiewDynamiqueActivity extends ListActivity {
TextView selection;
String[] contact0=new String[500] ; // ##nb de ligne a voir

int i=0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new IconicAdapter(this));
selection=(TextView)findViewById(R.id.selection);
//code juste pour afficher le nom dans la Listview

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
Log.v("TAG", "valeur du string:"+contact);
contact0=contact ;
i++;
}
people.close();

}
public void onListItemClick(ListView parent, View v,
int position, long id) {
selection.setText(contact0[position]);
}
class IconicAdapter extends ArrayAdapter {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.row, contact0);
this.context=context;
}

public View getView(int position, View convertView,ViewGroup parent) {
View row=convertView;
if (row==null) {
LayoutInflater inflater=context.getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
}
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(contact0[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
if (contact0[position].length()>4) {
icon.setImageResource(R.drawable.pouce_gauche_affectif_off);
}
else {
icon.setImageResource(R.drawable.ic_launcher);
}
return(row);
}
}
}


Mais ca ne correspond pas vraiment à ce que je voulais faire.
A la base l'idée générale est de pouvoir sélectionner puis enregistrer des informations dans le contact directement (je n'ai encore trouvé aucun tuto sur ce sujet).
Donc je suppose que le passage par un objet Contact comme tu le conseillais est plus approprié.
Comme je compte faire des modifications sur les contacts, je suppose que j'ai également besoin du Contact_ID d'après ce que j'ai pu lire.

En repartant du code que tu m'avais donné, je ne comprends pas comment faire le lien entre ce dernier et la listview à proprement parlé.


private List<My_contact> getContacts(){
List<My_contact>contacts=new ArrayList<My_contact>();
My_contact currentContact;
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
currentContact=new My_contact();
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
currentContact.set_nom_contact(contact);
contacts.add(currentContact);
}
people.close();
return contacts;
}

En tout cas voici mon objet contact

package list.view.dyna;
public class My_contact {

private String prenom_contact;
private String nom_contact;
private String telephone_contact;
private String email_contact;
private String droitier_contact;
private String profil_bras_contact;

public My_contact(){
prenom_contact="Inconnu";
nom_contact="Inconnu";
telephone_contact="Inconnu";
email_contact="Inconnu";
droitier_contact="1";
profil_bras_contact="Inconnu";
}

public String get_prenom_contact(){
return prenom_contact;
}
public String get_nom_contact(){
return nom_contact;
}
public String get_telephone_contact(){
return telephone_contact;
}
public String get_email_contact(){
return email_contact;
}
public String get_droitier_contact(){
return droitier_contact;
}
public String get_profil_bras_contact(){
return profil_bras_contact;
}

public void set_prenom_contact(String prenom_contact) {
this.prenom_contact = prenom_contact;
}
public void set_nom_contact(String nom_contact) {
this.nom_contact = nom_contact;
}
public void set_telephone_contact(String telephone_contact) {
this.telephone_contact = telephone_contact;
}
public void set_email_contact(String email_contact) {
this.email_contact = email_contact;
}
public void set_droitier_contact(String droitier_contact) {
this.droitier_contact = droitier_contact;
}
public void set_profil_bras_contact(String profil_bras_contact) {
this.profil_bras_contact = profil_bras_contact;
}
}

Encore Merci

Please Log in or Create an account to join the conversation.

Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Affichage
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline