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 **********************/
} |
Partager