IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants graphiques Android Discussion :

AlertDialog et clic sur listview


Sujet :

Composants graphiques Android

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut AlertDialog et clic sur listview
    Bonjour

    Je voudrais afficher un AlertDialog personnalisé à partir d'un clic sur un item d'un listview, j'ai donc créé un custom_dialog.xml

    je récupère le string nom de l'item

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    @Override
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		Cursor cursor=(Cursor)l.getAdapter().getItem(position);
    		String nom=cursor.getString(cursor.getColumnIndex("nom"));
    		FicheDialog();
    		super.onListItemClick(l, v, position, id);
    	}
    Ensuite je créé une procédure FicheDialog()

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public void FicheDialog(){
    		AlertDialog.Builder ad = new AlertDialog.Builder(this);
    		ad.setTitle("Bonjour");
     
    		ad.setView(LayoutInflater.from(this).inflate(R.layout.custom_dialog, null));
     
     
     
    		ad.show();
    	}
    Je n'arrive pas a écrire le code pour afficher le String nom dans la textview text de mon layout.custom_dialog.

    Ce que souhaierais c'est à partir du String nom, c'est créer un databind pour afficher plusieurs infos sur mon layout.custom_dialog

    Pouvez-vous me guider ?
    Merci pour vosréponses
    JCM

  2. #2
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    @Override
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		Cursor cursor=(Cursor)l.getAdapter().getItem(position);
    		String nom=cursor.getString(cursor.getColumnIndex("nom"));
    		FicheDialog(nom);
    		super.onListItemClick(l, v, position, id);
    	}
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public void FicheDialog(String nom){
    		AlertDialog.Builder ad = new AlertDialog.Builder(this);
    		ad.setTitle("Bonjour " + nom);
                 ad.setView(LayoutInflater.from(this).inflate(R.layout.custom_dialog, null));
     
    		ad.show();
    	}
    ?

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    Merci pour ta réponse

    Ce que je veux faire, c'est attribuer des valeurs à R.layout.custom_dialog

    j'ai codé comme suit

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public void FicheDialog(String nom){
    		AlertDialog.Builder ad = new AlertDialog.Builder(this);
    		ad.setTitle(nom);
    		Cursor c = db.mafiche();
    		startManagingCursor(c);
    		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
    		R.layout.custom_dialog, c, new String[]
    		{"activite","logo"}, 
    		new int[]{R.id.myactivite,R.id.mylogo}
    		);
    		setListAdapter(adapter);                ad.setView(LayoutInflater.from(this).inflate(R.layout.custom_dialog, null));
    ad.show();
    	}
    Mais c'est pas bon, je n'ai pas d'affichage

  4. #4
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Personnellement, j'ai toujours pas vraiment compris ce que tu essayes de faire.
    Ton code dernièrement indiqué n'a pas vraiment de sens

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    Ma question est-elle stupide, que tu ne me repondes pas ?

  6. #6
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Non, je dis juste que je n'ai pas compris ta question.
    Je pensais que :
    - Tu as une liste qui affiche une série d'item.
    - Cette liste est à l'écoute des clics.
    - Lors d'un clic sur un item une popup s'ouvre.
    - Cette popup doit contenir des informations relatives à l'item cliqué.

    Mais à priori ce n'est pas ça.
    Peux-tu expliciter ton problème ?

  7. #7
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    j'ai une liste qui affiche une série d'item.
    - Cette liste est à l'écoute des clics.
    - Lors d'un clic sur un item une popup s'ouvre.

    Cette popup (R.layout.custom_dialog) doit contenir des informations relatives à l'item cliqué.

    comme par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    String activite=cursor.getString(cursor.getColumnIndex("activite"));
    String logo=cursor.getString(cursor.getColumnIndex("logo"));
    Srting logo est le nom d'une image, je ne vois pas comment l'affichée

  8. #8
    Membre chevronné

    Profil pro
    Inscrit en
    Février 2008
    Messages
    658
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 658
    Par défaut
    Citation Envoyé par MrDuChnok Voir le message

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public void FicheDialog(String nom){
    		AlertDialog.Builder ad = new AlertDialog.Builder(this);
    		ad.setTitle("Bonjour " + nom);
                 ad.setView(LayoutInflater.from(this).inflate(R.layout.custom_dialog, null));
    
    		ad.show();
    	}
    Si joint ce trouve le code de style de developpement de Java publié par Sun Microsystem.

  9. #9
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    Bonjour

    Merci pour vos réponses, j'ai bien écrit ce code, mais cela ne rempli pas custom_dialog.xml. Il doit manquer du code ?

    voici custom_dialog

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
      	<LinearLayout
      	xmlns:android="http://schemas.android.com/apk/res/android"
      	android:layout_width="fill_parent"
      	android:layout_height="wrap_content"
      	android:orientation="horizontal">
    	<ImageView
    		android:id="@+id/mylogo"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    	   	android:layout_gravity="center_vertical"
    	   	android:padding="10px"
    		/>
     	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	    android:orientation="vertical"
    	    android:layout_width="fill_parent"
    	    android:layout_height="wrap_content"
    	   	android:layout_gravity="center_vertical"
    	   	android:paddingLeft="10px"
    	    android:layout_weight="1"
    	    >
       	    <TextView android:id="@+id/mynom"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:textSize="16px"
    	         android:textStyle="bold"
    	         android:textColor="#FFFFFF"/>
     		<TextView android:id="@+id/myactivite"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:layout_weight="1"
    	         android:textColor="#FFFFFF"
    	         android:textSize="12px"
    	         android:textStyle="bold"/>
    	    <TextView android:id="@+id/tel"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:layout_weight="1"
    	         android:textColor="#FFFFFF"
    	         android:textSize="12px"
    	         android:textStyle="bold"/>
    	</LinearLayout>
    </LinearLayout>
    <LinearLayout android:id="@+id/LinearLayout01" 
    	    android:layout_width="wrap_content" 
    	    android:layout_height="wrap_content">
    	    <TextView android:id="@+id/mydescro"    
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:layout_weight="1"
    	         android:textColor="#FFFFFF"
    	         android:textSize="12px"
    	         android:textStyle="bold"/>
         </LinearLayout>
     </LinearLayout>

  10. #10
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    Je me suis inspiré du lien http://developer.android.com/guide/t...ShowingADialog

    et j'ai modifié mon code comme ceci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public void FicheDialog(String nom){
    		AlertDialog.Builder builder;
    		AlertDialog.Builder ad = new AlertDialog.Builder(this);
    		ad.setTitle(nom);
    		Context mContext = getApplicationContext();
    		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    		View layout = inflater.inflate(R.layout.custom_dialog,                               
    				(ViewGroup) findViewById(R.id.layout_root));
    		TextView text = (TextView) layout.findViewById(R.id.mydescro);
    		text.setText("Hello, this is a custom dialog!");
    		ImageView image = (ImageView) layout.findViewById(R.id.mylogo);
    		image.setImageResource(R.drawable.van_2);
    		builder = new AlertDialog.Builder(mContext);
    		builder.setView(layout);
    		alertDialog = builder.create();
    ad.show();
    	}
    J'ai déclaré ma variable
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    private AlertDialog alertDialog;
    Mais cela m'affiche une boite vide (juste le titre affiché)

    Pouvez-vous m'indiquer l'erreur
    Merci

  11. #11
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Tu affiches la mauvaise fenêtre.
    Nettoie ton code, supprime ce qui est inutile et tu devrais retrouver facilement ton erreur.
    Pense à utiliser le debugueur pour ce genre de cas, tu aurais vu ton erreur en 5 secondes.

  12. #12
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    Je ne comprends pas : Tu affiches la mauvaise fenêtre.
    Nettoie ton code : mon code précisé ci-avant n'est pas bon ?
    Pense à utiliser le debugueur : je n'ai aucune ligne rouge sur le logcat

  13. #13
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Citation Envoyé par JCMANSION Voir le message

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public void FicheDialog(String nom){
    		AlertDialog.Builder builder;
    		AlertDialog.Builder ad = new AlertDialog.Builder(this);
    		ad.setTitle(nom);
    		Context mContext = getApplicationContext();
    		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    		View layout = inflater.inflate(R.layout.custom_dialog,                               
    				(ViewGroup) findViewById(R.id.layout_root));
    		TextView text = (TextView) layout.findViewById(R.id.mydescro);
    		text.setText("Hello, this is a custom dialog!");
    		ImageView image = (ImageView) layout.findViewById(R.id.mylogo);
    		image.setImageResource(R.drawable.van_2);
    		builder = new AlertDialog.Builder(mContext);
    		builder.setView(layout);
    		alertDialog = builder.create();
    ad.show();
    	}

  14. #14
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    Merci pour ce code

    mais cela n'affiche pas le View layout

  15. #15
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    367
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 367
    Par défaut
    J'ai beau chercher, mais la boite de dialogue s'affiche avec son titre mais sans le contenu du layout

Discussions similaires

  1. clic sur item listview fonctionne pas
    Par JCMANSION dans le forum Composants graphiques
    Réponses: 2
    Dernier message: 07/12/2010, 13h06
  2. [AC-2000] Utilisation Evenement clic sur ListView
    Par petitbison dans le forum IHM
    Réponses: 10
    Dernier message: 03/08/2009, 19h54
  3. Réponses: 8
    Dernier message: 09/05/2007, 00h35
  4. Réponses: 5
    Dernier message: 08/06/2006, 16h11
  5. capter l'evenement clic sur une cellule d'un string grid
    Par lasconic dans le forum Composants VCL
    Réponses: 3
    Dernier message: 25/06/2003, 10h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo