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 :

Problème de débutant avec setAdapter


Sujet :

Android

  1. #1
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 13
    Points : 12
    Points
    12
    Par défaut Problème de débutant avec setAdapter
    Salut,
    je commence à apprendre java sur Androïd et j'aimerai afficher une liste personnalisée. Mon appli plante et je ne trouve pas la raison.
    Ca doit être facile pour un pro sachant que je débute vraiment.

    Je vous remercie par avance du temps que vous pourriez me consacrer.

    liste_tanieres.java
    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
    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;
     
    public class liste_tanieres extends Activity {
     
    	ListView lvListe;
    	List<enigme> liste_enigmes = new ArrayList<enigme>();
     
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.liste_tanieres_layout);
     
    		lvListe = (ListView)findViewById(R.id.lvListe);
     
    		RemplirLalisteDesEnigmes();
     
    		EnigmeAdapter adapter = new EnigmeAdapter(this,liste_enigmes);
     
    		lvListe.setAdapter(adapter);
     
    		adapter.notifyDataSetChanged();
     
    	}
     
    	private void RemplirLalisteDesEnigmes(){
    		liste_enigmes.clear();
    		liste_enigmes.add(new enigme("Enigme 1",1,0));
    		liste_enigmes.add(new enigme("Enigme 2",2,0));
    		liste_enigmes.add(new enigme("Enigme 3",3,0));
    	}
     
    }
    enigme.java qui correspond à l'objet de ma liste à afficher
    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
    public class enigme {
     
    	private String titre;
     
    	private int numero;
     
    	private int resolue;
     
    	//Constructor
     
    	public enigme(String titre, int numero, int resolue){
    		this.titre = titre;
    		this.numero = numero;
    		this.resolue = resolue;
    	}
     
    	//Methods
     
    	public String getTitre(){
    		return titre;
    	}
     
    	public void setTitre(String titre){
    		this.titre = titre;
    	}
     
    	public int getNumero(){
    		return numero;
    	}
     
    	public void setNumero(int numero){
    		this.numero = numero;
    	}
     
    	public int getResolue(){
    		return resolue;
    	}
     
    	public void setResolue(int resolue){
    		this.resolue = resolue;
    	}
     
    }
    EnigmeAdapter
    l'erreur est sans doute ici :/
    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
    import java.util.List;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
     
    public class EnigmeAdapter extends BaseAdapter {
     
    	List<enigme> liste_enigmes;
    	LayoutInflater inflater;
     
    	public EnigmeAdapter(Context context,List<enigme> liste_enigmes) {
    		inflater = LayoutInflater.from(context);
    		this.liste_enigmes = liste_enigmes;
    	}
     
    	@Override
    	public int getCount() {
    		return liste_enigmes.size();
    	}
     
    	@Override
    	public Object getItem(int position) {
    		return liste_enigmes.get(position);
    	}
     
    	@Override
    	public long getItemId(int position) {
    		return position;
    	}
     
    	private class ViewHolder {
    		TextView tvTitre;
    		TextView tvNumero;
    	}
     
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder holder;
     
    		if(convertView == null) {
    			holder = new ViewHolder();
    			convertView = inflater.inflate(R.layout.itemenigme, null);
    			holder.tvTitre = (TextView)convertView.findViewById(R.id.tvTitre);
    			holder.tvNumero = (TextView)convertView.findViewById(R.id.tvNumero);
    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
     
    		holder.tvTitre.setText(liste_enigmes.get(position).getTitre());
    		holder.tvNumero.setText(liste_enigmes.get(position).getNumero());
     
    		return convertView;
    	}
     
    }
    itemenigme.xml
    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
     
        <TextView
            android:id="@+id/tvTitre"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
     
    	<TextView
    	android:id="@+id/tvNumero"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"/>
     
    	<TextView
    	android:id="@+id/tvResolue"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"/>
     
    </LinearLayout>
    liste_tanieres_layout.xml
    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
    <?xml version="1.0" encoding="utf-8"?>
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     
    	<ListView
    	android:id="@+id/lvListe"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent">
     
    	</ListView>
     
    </LinearLayout>

  2. #2
    Membre habitué
    Homme Profil pro
    Inscrit en
    Octobre 2011
    Messages
    281
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2011
    Messages : 281
    Points : 161
    Points
    161
    Par défaut
    Que dit le Logcat ?

  3. #3
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    je commence à apprendre java
    Alors autant commencer bien avec les conventions d'écriture en Java:
    En gros:
    • On n'utilise jamais "_" sauf éventuellement dans les noms de constantes.
    • Les noms de types (classes, enum, interface, ...) commencent par une lettre majuscule et sont en "CamelCase", par exemple: "EnigmeAdapter" ok, "enigme" pas ok, "liste_tanieres" pas ok du tout.
    • Les noms de types doivent (si possible) indiquer leur rôle, pour moi "liste_tanieres" n'est qu'une liste de tanieres, et pas du tout une activité affichant une liste d'enigmes "ListeEnigmeActivity" serait un meilleur nom.
    • Les noms de variables et fonctions (locales / membres ou statiques) doivent commencer par une minuscule, et sont en "camelCase": "lvListe" ok, "liste_enigmes" pas ok (le '_', devrait être "listeEnigmes"), "RemplirLalisteDesEnigmes();" devrait être "remplirLaListeDesEnigmes()"
    • Les noms de constantes sont en général tout en majuscule, chaque mot séparé par "_".



    Mon appli plante et je ne trouve pas la raison.
    Dans ce cas, il faut lire le "logcat"... celui-ci indiquera l'exception, et la "stacktrace" (la liste de tous les appels ayant amené à cette exception).


    De manière indépendante.... le premier "adapter.notifyDataSetChanged();" ne sert à rien...
    Cet appel n'est utile que si le contenu de l'adapter a été modifié alors que la liste est déjà affichée (c'est d'ailleurs peut-être ça qui plante).
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  4. #4
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 13
    Points : 12
    Points
    12
    Par défaut
    Citation Envoyé par anto2b Voir le message
    Que dit le Logcat ?
    Merci pour ta réponse.
    Un problème de ressource non trouvée ?

    Le logcat :
    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
    12-18 19:38:41.715: D/(2120): Device driver API match
    12-18 19:38:41.715: D/(2120): Device driver API version: 10
    12-18 19:38:41.715: D/(2120): User space API version: 10 
    12-18 19:38:41.715: D/(2120): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
    12-18 19:38:41.760: D/OpenGLRenderer(2120): Enabling debug mode 0
    12-18 19:38:43.870: D/GestureDetector(2120): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
    12-18 19:38:44.190: D/AbsListView(2120): Get MotionRecognitionManager
    12-18 19:38:44.250: W/ResourceType(2120): No package identifier when getting value for resource number 0x00000001
    12-18 19:38:44.250: D/AndroidRuntime(2120): Shutting down VM
    12-18 19:38:44.250: W/dalvikvm(2120): threadid=1: thread exiting with uncaught exception (group=0x41aff2a0)
    12-18 19:38:44.265: E/AndroidRuntime(2120): FATAL EXCEPTION: main
    12-18 19:38:44.265: E/AndroidRuntime(2120): android.content.res.Resources$NotFoundException: String resource ID #0x1
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.content.res.Resources.getText(Resources.java:242)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.TextView.setText(TextView.java:3805)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at com.wickedpanda.www.EnigmeAdapter.getView(EnigmeAdapter.java:56)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.AbsListView.obtainView(AbsListView.java:2468)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.ListView.makeAndAddView(ListView.java:1775)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.ListView.fillDown(ListView.java:678)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.ListView.fillFromTop(ListView.java:739)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.ListView.layoutChildren(ListView.java:1628)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.AbsListView.onLayout(AbsListView.java:2303)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.View.layout(View.java:14063)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewGroup.layout(ViewGroup.java:4655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.View.layout(View.java:14063)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewGroup.layout(ViewGroup.java:4655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.View.layout(View.java:14063)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewGroup.layout(ViewGroup.java:4655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.View.layout(View.java:14063)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewGroup.layout(ViewGroup.java:4655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.View.layout(View.java:14063)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewGroup.layout(ViewGroup.java:4655)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2017)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1838)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4611)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.Choreographer.doFrame(Choreographer.java:525)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.os.Handler.handleCallback(Handler.java:615)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.os.Handler.dispatchMessage(Handler.java:92)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.os.Looper.loop(Looper.java:137)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.app.ActivityThread.main(ActivityThread.java:4898)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at java.lang.reflect.Method.invokeNative(Native Method)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at java.lang.reflect.Method.invoke(Method.java:511)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at dalvik.system.NativeStart.main(Native Method)
    12-18 19:38:54.070: I/Process(2120): Sending signal. PID: 2120 SIG: 9

  5. #5
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 13
    Points : 12
    Points
    12
    Par défaut
    Citation Envoyé par nicroman Voir le message
    Alors autant commencer bien avec les conventions d'écriture en Java:
    Merci. Je prends note de tes remarques sur les conventions. C'est vrai que c'est important pour plus de clarté.

    Je ne connaissais pas le logcat, (ne pas rire svp ^^) Je vais voir comment ça fonctionne sur le net.

    Bonne journée.

  6. #6
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Comme quoi avec le logcat tout est clair !
    Dans le thread principal de l'application, on a rencontré une exception non gérée (fatal)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    12-18 19:38:44.265: E/AndroidRuntime(2120): android.content.res.Resources$NotFoundException: String resource ID #0x1
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.content.res.Resources.getText(Resources.java:242)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at android.widget.TextView.setText(TextView.java:3805)
    12-18 19:38:44.265: E/AndroidRuntime(2120): 	at com.wickedpanda.www.EnigmeAdapter.getView(EnigmeAdapter.java:56)
    Cette exception est "Resources.NotFoundException" (en bref, "j'ai pas trouvé la ressource") pour la ressource de type "string" et d'identifiant #0x1
    Cette exception s'est produite dans android.content.res.Resources.getText(...) (ok)
    appelé par android.widget.TextView.setText(...) (ok)
    appelé par com.wickedpanda.www.EnigmeAdapter.getView(...) (<= c'est là qu'on est chez nous !)
    Et il y a même le nom de fichier (EnigmeAdapter.java) et la ligne (56) !

    A cette ligne on doit surement trouver:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    holder.tvNumero.setText(liste_enigmes.get(position).getNumero());
    Hors, la fonction setText(int) prend comme paramètre un identifiant de *ressource* de type "string"....
    A ne pas confondre avec setText(CharSequence) qui prend directement une chaîne de caractères à afficher.

    Là tu veux convertir un entier en chaîne donc il manque une étape:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    holder.tvNumero.setText(Integer.toString(liste_enigmes.get(position).getNumero()));
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  7. #7
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 13
    Points : 12
    Points
    12
    Par défaut
    Un grand merci !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problèmes en débutant avec Zend Framework
    Par Greg71 dans le forum MVC
    Réponses: 2
    Dernier message: 14/12/2009, 23h36
  2. Petit problème de débutant avec les tableaux
    Par crazy_zakaria dans le forum Débuter
    Réponses: 10
    Dernier message: 20/11/2009, 10h25
  3. Problème [gros débutant!] avec malloc
    Par Nival dans le forum Débuter
    Réponses: 5
    Dernier message: 13/03/2009, 17h05
  4. problème de débutant avec la librairie glut sous dev C++
    Par mozillo3625 dans le forum Windows
    Réponses: 0
    Dernier message: 30/11/2007, 22h56
  5. [C#][service windows] problème de débutant avec 1 timer
    Par Nycos62 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 14/10/2005, 11h22

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