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

Android Discussion :

Besoin d'aide pour mon application


Sujet :

Android

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut Besoin d'aide pour mon application
    Bonjour à tous, alors je suis en train de coder une application avec un nombre de "if".
    J'ai pas mal d'images et je veux qui si l'image apparaît alors faire ...
    Voici mon code actuel :



    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
    public class MainActivity extends ActionBarActivity {
     
     
     private final static Random random = new Random();
     private Button next;
     private ImageView cardImage,cardImage2;
     int currentCardIndex = 0;    
     
     
     int[] cards = {
         R.drawable.h2,  
         R.drawable.h3,  
         R.drawable.h4,  
         R.drawable.h5,  
         R.drawable.h6,
         R.drawable.h7,
         R.drawable.h8,
         R.drawable.h9,
         R.drawable.h10,
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout root = (LinearLayout) findViewById(R.id.clickscreen);
        shuffle(cards);
        next  = (Button)findViewById(R.id.button1);
        cardImage   = (ImageView)findViewById(R.id.cards);
        final Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.abc_slide_in_top);
        new Dialog(getApplicationContext());
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.siren);
        final MediaPlayer card = MediaPlayer.create(this, R.raw.cardflip);
     
        root.setOnClickListener(new OnClickListener() {
     
            public void onClick(View v) {
     
                cardImage.startAnimation(fadeInAnimation);
                cardImage.setImageResource(cards[currentCardIndex++]);
                card.start();
     
     
                 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ta).getConstantState())){
                       AlertUtil.messageAlert(MainActivity.this,getString(R.string.cul_sec) , getString(R.string.cul_sec_regle));
     
                      }  
                 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.sa).getConstantState())){
                       AlertUtil.messageAlert(MainActivity.this,getString(R.string.cul_sec), getString(R.string.cul_sec_regle));
     
                 }
                if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ha).getConstantState())){
                       AlertUtil.messageAlert(MainActivity.this,getString(R.string.cul_sec), getString(R.string.cul_sec_regle));
                      }

    Alors je voulais savoir s'il y a une manière plus simple et qui ferait que ça ne lag pas ?
    Dans le code au-dessus je n'ai pas mis tout mes IF sinon ça ferait trop long.
    Merci d'avance !

  2. #2
    Membre actif
    Homme Profil pro
    Consultant BigData
    Inscrit en
    Juillet 2009
    Messages
    129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Consultant BigData

    Informations forums :
    Inscription : Juillet 2009
    Messages : 129
    Points : 280
    Points
    280
    Par défaut
    Bonjour,

    Etant donné que le cœur du if semble toujours le même, je te conseillerais de rentrer ces éléments dans deux HashMap une et une seule fois au lancement (en gros, dans le onCreate, mais pas dans le onClick).
    Ensuite, l'intégralité de tes ifs serait remplacé par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    AlertUtil.messageAlert(MainActivity.this,hashmapString1.get(cardImage.getDrawable().getConstantState()) , hashmapString2.get(cardImage.getDrawable().getConstantState()));
    Un exemple pour l'initialisation avec le premier if :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Map<ConstantState, String> hashmapString1 = new HashMap<ConstantState,String>();
    Map<ConstantState, String> hashmapString2 = new HashMap<ConstantState,String>();
    hashmapString1.put(getResources().getDrawable(R.drawable.ta).getConstantState(), getString(R.string.cul_sec));
    hashmapString2.put(getResources().getDrawable(R.drawable.ta).getConstantState(), getString(R.string.cul_sec_regle));
    Bien sur, je te conseille aussi de trouver des meilleurs noms pour ces hashmap qui reflètent ce que tu veux mettre dedans.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut
    Merci ça fonctionne,
    c'est long a tapé mais bon...
    sinon est il possible de comparer la première lettre des drawable ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    hashmapString1.put(getResources().getDrawable(R.drawable.Xa).getConstantState(), getString(R.string.cul_sec));
            hashmapString2.put(getResources().getDrawable(R.drawable.Xa).getConstantState(), getString(R.string.cul_sec_regle));

    Par exemple tous ce qui ce termine par la lettre a alors

    Merci pour ta réponse

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut
    Je ne suis pas sur de bien comprendre
    ce que tu m'as dit fonctionne pour le premier if après je fait comment ?

    Comme ça ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    final Map<ConstantState, String> hashmapString1 = new HashMap<ConstantState,String>();
            final Map<ConstantState, String> hashmapString2 = new HashMap<ConstantState,String>();
            hashmapString1.put(getResources().getDrawable(R.drawable.ta).getConstantState(), getString(R.string.cul_sec));
            hashmapString2.put(getResources().getDrawable(R.drawable.ta).getConstantState(), getString(R.string.cul_sec_regle));
            final Map<ConstantState, String> hashmapString3 = new HashMap<ConstantState,String>();
            final Map<ConstantState, String> hashmapString4 = new HashMap<ConstantState,String>();
            hashmapString3.put(getResources().getDrawable(R.drawable.sa).getConstantState(), getString(R.string.cul_sec));
            hashmapString4.put(getResources().getDrawable(R.drawable.sa).getConstantState(), getString(R.string.cul_sec_regle));

    Et dans Onclick :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    			public void onClick(View v) {
     
    				cardImage.startAnimation(fadeInAnimation);
    				cardImage.setImageResource(cards[currentCardIndex++]);
    				card.start();
    				AlertUtil.messageAlert(MainActivity.this,hashmapString1.get(cardImage.getDrawable().getConstantState()) , hashmapString2.get(cardImage.getDrawable().getConstantState()));
    				AlertUtil.messageAlert(MainActivity.this,hashmapString3.get(cardImage.getDrawable().getConstantState()) , hashmapString4.get(cardImage.getDrawable().getConstantState()));

    Je ne suis pas sur de bien faire
    Merci d'avance

  5. #5
    Membre actif
    Homme Profil pro
    Consultant BigData
    Inscrit en
    Juillet 2009
    Messages
    129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Consultant BigData

    Informations forums :
    Inscription : Juillet 2009
    Messages : 129
    Points : 280
    Points
    280
    Par défaut
    Tu ne dois déclarer que 2 maps en tout.
    Par exemple, pour ton second item :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    hashmapString1.put(getResources().getDrawable(R.drawable.sa).getConstantState(), getString(R.string.cul_sec));
    hashmapString2.put(getResources().getDrawable(R.drawable.sa).getConstantState(), getString(R.string.cul_sec_regle));
    En gros, dans la première hashmap, tu entreras tous les éléments qui doivent être passés en second argument de ton "messageAlert" et dans la seconde ceux du troisième argument.

    Edit : et il ne doit y avoir du coup qu'un seul appel à "messageAlert" en replacement de tous les ifs.

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut
    Ok merci je vais essayer ça

    Merci pour vous réponse !

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut
    Ah alors ça fonctionne niquel,
    Mais j'avais certaine condition qui ouvrer une autre activity ou lancer de la musique comment faire du coup?

    C’était comme ça :

    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
     if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.d9).getConstantState())){
    					 startActivity(new Intent(v.getContext(),Combat_Des.class));
    	                  }
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.t9).getConstantState())){
    					 startActivity(new Intent(v.getContext(),Combat_Des.class));
    	                  }
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.s9).getConstantState())){
    					 startActivity(new Intent(v.getContext(),Combat_Des.class));
    	                  }
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.h9).getConstantState())){
    					 startActivity(new Intent(v.getContext(),Combat_Des.class));     
    				 }
     
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.dj).getConstantState())){
    	           		   AlertUtil.messageAlert(MainActivity.this,getString(R.string.bizkit), getString(R.string.bizkit_regle));
    	           		mp.start();
    				 }
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.tj).getConstantState())){
    	           		   AlertUtil.messageAlert(MainActivity.this,getString(R.string.bizkit), getString(R.string.bizkit_regle));
    	           		mp.start();
    				 }
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.sj).getConstantState())){
    	           		   AlertUtil.messageAlert(MainActivity.this,getString(R.string.bizkit), getString(R.string.bizkit_regle));
    	           		mp.start();
    				 }
    				 if (cardImage.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.hj).getConstantState())){
    	           		   AlertUtil.messageAlert(MainActivity.this,getString(R.string.bizkit), getString(R.string.bizkit_regle));      
    	           		mp.start();
    				 }
    Merci a vous

  8. #8
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut
    En faite je laisse comme ça.

    Merci beaucoup a vous c'est beaucoup mieux !

    Merci

  9. #9
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2014
    Messages : 33
    Points : 27
    Points
    27
    Par défaut
    J'ai réussi à fini mon application merci.
    Mais petit souci j'ai remarquer quelle ne ce lance pas sur tous les appareils, avez vous une idée du pourquoi ?

    Voici le lien Playstore : https://play.google.com/store/apps/d....drinkingcards

Discussions similaires

  1. [XML] besoin de votre aide pour mon application de satge d'ete
    Par minoule1 dans le forum Bibliothèques et frameworks
    Réponses: 0
    Dernier message: 14/09/2012, 01h58
  2. Besoin d'aide pour mon application
    Par Slackk dans le forum VB.NET
    Réponses: 4
    Dernier message: 17/06/2011, 12h00
  3. Besoin d'aide pour mon appli
    Par vince351 dans le forum AWT/Swing
    Réponses: 22
    Dernier message: 31/01/2007, 12h26
  4. besoin d'aide pour mon future site
    Par vulquin dans le forum Général Conception Web
    Réponses: 3
    Dernier message: 11/06/2006, 20h33
  5. Besoin d'aide pour mon rpg en javascript
    Par CyberTwister dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 07/04/2006, 13h26

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