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 :

ListView dans Onglets


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2011
    Messages : 10
    Par défaut ListView dans Onglets
    Bonjour,

    J'essai de mettre en place une appli avec 4 onglets avec pour chaque onglet une liste qui est renvoyée par un webservice. Le code ci-dessous présente la mise en place des 4 onglets et le remplissage des 4 listes. Cependant avec ce script, quand je change d'onglet la liste se remplit mais ne s'affiche pas...

    ((Aucun message d'erreur dans les logs))

    Comment puis-je faire pour afficher la bonne liste à chaque changement d'onglet ? Je pense qu'il faut que j'externalise des class mais je ne vois pas trop quoi et comment...

    Merci par avance pour votre aide.

    Cordialement.

    Script 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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
     
    package com.mon.appli;
     
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONObject;
     
    import android.app.ListActivity;
    import android.net.http.AndroidHttpClient;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.TabHost;
    import android.widget.Toast;
    import android.widget.TabHost.TabSpec;
     
    public class MonActivityPrincipal extends ListActivity {
     
    	private static ArrayList<Musique> ListeMorceaux = new ArrayList<Musique>();
    	private static ArrayList<Album> ListeAlbums = new ArrayList<Album>();
    	private static ArrayList<Artiste> ListeArtistes = new ArrayList<Artiste>();
    	private static ArrayList<Playlist> ListePlaylists = new ArrayList<Playlist>();
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
            /************** CREATION DE LA LISTE LISTVIEW *********************************/
            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    new ArrayList<String>()));
            new AjoutItemListeTitres().execute("onglet_1");
     
            /************** FIN CREATION DE LA LISTE LISTVIEW *********************************/
     
            /******************* CREATION DES ONGLETS **************************************/
            //Récupération du TabHost
            TabHost monTabHost = (TabHost)findViewById(R.id.TabHost01);
            // Avant d'ajouter des onglets, il faut impérativement appeler la methode setup() du TabHost
            monTabHost.setup();
     
            // Nous ajoutons les 3 onglets dans notre TabHost
     
            // Nous paramétrons le 1er onglet
            TabSpec spec = monTabHost.newTabSpec("Onglet_1");
            // Nous parametrons le texte qui s'affichera dans l'onglet
            // ainsi que l'image qui se positionnera au dessus du texte
     
            spec.setIndicator(getString(R.string.onglet_1));
            // ON spécifie le layout qui s'affichera lorsque l'onglet sera selectionné
            spec.setContent(R.id.Onglet1); // ici je dois faire un truc genre : new Intent(this, ListeTitres.class) je crois
            // ON ajoute l'onglet dans notre TabHost
            monTabHost.addTab(spec);
     
            // Vous pouvez ajouter des onglets comme ceci :
            monTabHost.addTab(monTabHost.newTabSpec("onglet_2").setIndicator(getString(R.string.onglet_2)).setContent(R.id.Onglet2));
            monTabHost.addTab(monTabHost.newTabSpec("onglet_3").setIndicator(getString(R.string.onglet_3)).setContent(R.id.Onglet3));
            monTabHost.addTab(monTabHost.newTabSpec("onglet_4").setIndicator(getString(R.string.onglet_4)).setContent(R.id.Onglet4));
            // Nous parametrons un ecouteur onTabChangedListener pour recuperer le changement d'onglet
            monTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
     
    			public void onTabChanged(String tabId) {
    				// vous pourrez executer du code lorsqu'un onglet est cliqué. Pour déterminer quel onglet
    				// a été cliqué, il vous suffira de vérifier le tabId envoyé lors du clic et d'exécuter votre
    				// code en conséquence
    				Toast.makeText(MonActivityPrincipal .this, "L'onglet avec l'identifiant : " + tabId + " a été cliqué", Toast.LENGTH_SHORT).show();
     
    				setListAdapter(new ArrayAdapter<String>(MonActivityPrincipal.this,
    			                android.R.layout.simple_list_item_1,
    			                new ArrayList<String>()));
    				new AjoutItemListeTitres().execute(tabId);
    			}
    		});
            /******************* FIN CREATION DES ONGLETS **************************************/
        }
     
     
        /************************** GESTION DU REMPLISSAGE DES LISTES *****************/
        class AjoutItemListeTitres extends AsyncTask<String, Object, Void> {
        	protected Void doInBackground(String... param) {
        		try{
         	       // Création du httpclient android acceptant le ssl
         		   AndroidHttpClient httpclient = AndroidHttpClient.newInstance("Android");
     
         		   HttpPost httpPost = null;
         		   if(param[0] == "onglet_1"){
         			  httpPost = new HttpPost(getString(R.string.url_get_liste_titres));
         		   }else if(param[0] == "onglet_2"){
          			  httpPost = new HttpPost(getString(R.string.url_get_liste_albums));
          		   }else if(param[0] == "onglet_3"){
           			  httpPost = new HttpPost(getString(R.string.url_get_liste_artistes));
           		   }else if(param[0] == "onglet_4"){
           			  httpPost = new HttpPost(getString(R.string.url_get_liste_playlists));
           		   }
     
         	       // Préparation des parametres post
         	       List<NameValuePair> parametres = new ArrayList<NameValuePair>();
         	       parametres.add(new BasicNameValuePair("a", "3"));
         	       // On encode les parametres et on les attache au httpPost (l'url) 
         	       UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parametres);
         	       httpPost.setEntity(formEntity);
         	       //Execution de la requete + Récup de reponse...
         	       HttpResponse httpResponse = httpclient.execute(httpPost);
         	       HttpEntity httpEntity = httpResponse.getEntity();
     
         	       if(httpEntity != null){
         	    	   InputStream resultatPage = httpEntity.getContent();  
         	    	   // Lecture du retour au format json
         	    	   BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultatPage));
         	    	   StringBuilder stockageReponse = new StringBuilder();
         	    	   // Stockage de la réponse dans le stockageReponse
         	    	   String lignelue = bufferedReader.readLine();
         	    	   while(lignelue != null){
         	    		   stockageReponse.append(lignelue + "\n");
         	    		   lignelue = bufferedReader.readLine();
         	    	   }
         	    	   bufferedReader.close();
     
         	    	   // Analyse de la reponse...
         	    	   JSONObject jsonObject = new JSONObject(stockageReponse.toString());
     
    	     	    	   if(param[0] == "onglet_1"){ // Cas des titres - onglet 1
    	     	    		   // Recup info titre
    	        	    	   JSONArray jsonMusique = jsonObject.getJSONArray("musique");
    	        	    	   // Initialisation variable
    	        	    	   int id_musique = 0;
    	        	    	   String titre_musique = "";
    	        	    	   String url = "";
    	        	    	   int id_album = 0;
    	        	    	   for(int i=0; i<jsonMusique.length()-1;i++){
    		          	    	   id_musique = jsonMusique.getJSONObject(i).getInt("id");
    		          	    	   titre_musique = jsonMusique.getJSONObject(i).getString("titre");
    		          	    	   url = jsonMusique.getJSONObject(i).getString("url");
    		          	    	   id_album = jsonMusique.getJSONObject(i).getInt("id_album");
    		          	    	   Musique morceau = new Musique(id_musique,titre_musique,url,id_album);
    		          	    	   publishProgress(morceau);
    		          	    	   ListeMorceaux.add(morceau);
    		          	    	   Log.i("Info morceau : ", "Titre morceau : "+ ListeMorceaux.get(i).get_titre_musique());
    	        	    	   }
    	         		   }else if(param[0] == "onglet_2"){ // Cas des albums - onglet 2
    	         			   // Recup info titre
    	        	    	   JSONArray jsonMusique = jsonObject.getJSONArray("album");
    	        	    	   // Initialisation variable
    	        	    	   int id_album = 0;
    	        	    	   String titre_album = ""; 
    	        	    	   int id_artiste = 0;
    	        	    	   for(int i=0; i<jsonMusique.length()-1;i++){
    	        	    		   id_album = jsonMusique.getJSONObject(i).getInt("id_album");
    		          	    	   titre_album = jsonMusique.getJSONObject(i).getString("nom_album");
    		          	    	   id_artiste = jsonMusique.getJSONObject(i).getInt("id_artiste");
    		          	    	   Album album = new Album(id_album,titre_album,id_artiste);
    		          	    	   publishProgress(album);
    		          	    	   ListeAlbums.add(album);
    		          	    	   Log.i("Info Album : ", "Titre album : "+ ListeAlbums.get(i).get_titre_album());
    	        	    	   }
    	          		   }else if(param[0] == "onglet_3"){ // Cas des artistes - onglet 3
    	          			   // Recup info titre
    	        	    	   JSONArray jsonMusique = jsonObject.getJSONArray("artiste");
    	        	    	   // Initialisation variable
    	        	    	   int id_artiste = 0;
    	        	    	   String nom_artiste = ""; 
    	        	    	   for(int i=0; i<jsonMusique.length()-1;i++){
    	        	    		   id_artiste = jsonMusique.getJSONObject(i).getInt("id_artiste");
    		          	    	   nom_artiste = jsonMusique.getJSONObject(i).getString("nom_artiste");
    		          	    	   Artiste artiste = new Artiste(id_artiste,nom_artiste);
    		          	    	   publishProgress(artiste);
    		          	    	   ListeArtistes.add(artiste);
    		          	    	   Log.i("Info Artiste : ", "Nom artiste : "+ ListeArtistes.get(i).get_nom_artiste());
    	        	    	   }
    	           		   }else if(param[0] == "onglet_4"){ // Cas des playlists - onglet 4
    	           			   // Recup info titre
    	        	    	   JSONArray jsonMusique = jsonObject.getJSONArray("playlist");
    	        	    	   // Initialisation variable
    	        	    	   int id_playlist = 0;
    	        	    	   String titre_playlist = ""; 
    	        	    	   for(int i=0; i<jsonMusique.length()-1;i++){
    	        	    		   id_playlist = jsonMusique.getJSONObject(i).getInt("id_playlist");
    	        	    		   titre_playlist = jsonMusique.getJSONObject(i).getString("titre_playlist");
    		          	    	   Playlist playlist = new Playlist(id_playlist,titre_playlist);
    		          	    	   publishProgress(playlist);
    		          	    	   ListePlaylists.add(playlist);
    		          	    	   Log.i("Info Playlist : ", "Nom PLaylist : "+ ListePlaylists.get(i).get_titre_playlist());
    	        	    	   }
    	           		   }
         	       }
         	       httpclient.close();
         		} catch(Exception e){
         			 Log.e("Erreur","Récup info impossible  :"+ e);
         		}
     
              return(null);
            }
     
            @Override
            protected void onProgressUpdate(Object... item) {
              String label = new String();
              if (item[0] instanceof Musique) {
            	  label = ((Musique) item[0]).get_titre_musique();
              }else if (item[0] instanceof Album){
            	  label = ((Album) item[0]).get_titre_album();
              }else if (item[0] instanceof Artiste){
            	  label = ((Artiste) item[0]).get_nom_artiste();
              }else if (item[0] instanceof Playlist){
            	  label = ((Playlist) item[0]).get_titre_playlist();
              }
              ((ArrayAdapter<String>)getListAdapter()).add(label);
            }
     
     
            @Override
            protected void onPostExecute(Void unused) {
            	Toast
                .makeText(MonActivityPrincipal.this, "Chargement complet!", Toast.LENGTH_SHORT)
                .show();
            }
          } 
        /************************* FIN DU REMPLISSAGE DES LISTES **********************/
    }
    main.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
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
     
    <?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="fill_parent">
     
    	<!-- Le TabHost qui contient tous les éléments de nos onglets -->
        <TabHost 
            android:id="@+id/TabHost01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
     
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
     
                <!-- TabWidget qui sert à afficher les onglets -->
                <TabWidget 
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </TabWidget>
                <!-- Contenu de nos onglets -->
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
     
                    <!-- Contenu de l'onglet 1 -->
                    <LinearLayout
                        android:id="@+id/Onglet1"
                        android:orientation="vertical"
    		            android:layout_width="fill_parent"
    		            android:layout_height="fill_parent">
    		                <ListView
    						    android:id="@android:id/list"
    						    android:layout_width="fill_parent" 
    						    android:layout_height="fill_parent"
    						    android:drawSelectorOnTop="false" />
                    </LinearLayout>
     
                    <!-- Contenu de l'onglet 2  -->
                   <LinearLayout
                        android:id="@+id/Onglet2"
                        android:orientation="vertical"
    		            android:layout_width="fill_parent"
    		            android:layout_height="fill_parent">
    		                <ListView
    						    android:id="@+id/list"
    						    android:layout_width="fill_parent" 
    						    android:layout_height="fill_parent"
    						    android:drawSelectorOnTop="false" />
                    </LinearLayout>
     
                    <!-- Contenu de l'onglet 3 -->
                    <LinearLayout
                        android:id="@+id/Onglet3"
                        android:orientation="vertical"
    		            android:layout_width="fill_parent"
    		            android:layout_height="fill_parent">
    		                <ListView
    						    android:id="@+id/list"
    						    android:layout_width="fill_parent" 
    						    android:layout_height="fill_parent"
    						    android:drawSelectorOnTop="false" />
                    </LinearLayout>
     
                    <!-- Contenu de l'onglet 4 -->
                   <LinearLayout
                        android:id="@+id/Onglet4"
                        android:orientation="vertical"
    		            android:layout_width="fill_parent"
    		            android:layout_height="fill_parent">
    		               <ListView
    						    android:id="@+id/list"
    						    android:layout_width="fill_parent" 
    						    android:layout_height="fill_parent"
    						    android:drawSelectorOnTop="false" />
                    </LinearLayout>
     
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    </LinearLayout>

  2. #2
    Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2014
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2014
    Messages : 2
    Par défaut
    Bonjour,

    Excusez moi de relancer cette discussion 4 ans plus tard, mais comment le problème a-t-il été résolu?
    J'utilise un code à peu près similaire pour mettre une listview dans un tabhost, mais le problème c'est qu'à l'affichage elle recouvre même les onglets

    Merci d'avance!

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

Discussions similaires

  1. Image dans onglet
    Par xtiand4 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 24
    Dernier message: 22/11/2011, 12h24
  2. [VB.NET] Exporter une listView dans un txt
    Par hm1ch dans le forum Windows Forms
    Réponses: 4
    Dernier message: 10/07/2006, 19h17
  3. Réponses: 41
    Dernier message: 17/05/2006, 17h01
  4. Réponses: 8
    Dernier message: 12/05/2006, 14h04
  5. [VB.NET] Image dans onglet
    Par olbi dans le forum Windows Forms
    Réponses: 1
    Dernier message: 09/05/2006, 17h02

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