Aller au contenu

Recommended Posts

Posté(e) (modifié)

Bonjour, voila mon problème j'ai une ListView et dans cette même ListView j'aimerai lancer DIFFERENTES activity je m'explique ( voir le code ci dessous en même temps ) :

Les deux premiers Items doivent lancer un PDF mais le troisième "Sur Facebook" doit lancer une nouvelle activity , plus précisément une nouvelle vue (facebook.java), j'arrive à le faire avec un bouton mais je n'arrive pas depuis ma ListView, voila mon bout de code :

package com.androiddev.tab;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import java.util.ArrayList;
import java.util.HashMap;
import com.androiddev.tab.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Tab5 extends ListActivity implements OnClickListener {
private ListView maListViewPerso25;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	setContentView(R.layout.onglet5);

	Button button = (Button) findViewById(R.id.imageButtonSelector2);
	button.setOnClickListener(this);


	maListViewPerso25 = (ListView) findViewById(R.id.listView25);
	ArrayList<HashMap<String, String>> listItem25 = new ArrayList<HashMap<String, String>>();
	HashMap<String, String> map;
	map = new HashMap<String, String>();
	map.put("titre", "1 - Qui sommes-nous ?");
	map.put("img", String.valueOf(R.drawable.fleche));
	map.put("file", "QSN.pdf");
	listItem25.add(map);
	map = new HashMap<String, String>();
	map.put("titre", "2 - A propos de l'application");
	map.put("img", String.valueOf(R.drawable.fleche));
	map.put("file", "Apropos.pdf");
	listItem25.add(map);
	map = new HashMap<String, String>();
	map.put("titre", "3 - Sur Facebook");
	map.put("img", String.valueOf(R.drawable.fleche));
	listItem25.add(map);
	SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem25, R.layout.afichageitem,
	  new String[] {"img", "titre" }, new int[] {R.id.fleche, R.id.titre});
	maListViewPerso25.setAdapter(mSchedule);


	//LISTENER


	maListViewPerso25.setOnItemClickListener(new OnItemClickListener() {
	   public void onItemClick(AdapterView<?> a, View v, int position, long id) {
		 HashMap<String, String> map = (HashMap<String, String>) maListViewPerso25.getItemAtPosition(position);
		 String nomFichierDansAsset = map.get("file");
		 String nomFichierTemp = "list25.pdf";


		 if (copyAssetToTempFile(nomFichierDansAsset, nomFichierTemp)) {
		  try {
			String name = getFileStreamPath(nomFichierTemp).getAbsolutePath();
			Uri uri = Uri.parse("file://" + name);
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(uri, "application/pdf");
			startActivity(intent);
		  } catch (ActivityNotFoundException e) {
			// Cas d'erreur si pas de lecteur PDF installé
			Log.d("xx", "Erreur affichage PDF", e);
		  }
		}
	  }
	 });


	 };
 private boolean copyAssetToTempFile(String nomFichierAsset,
   String nomFichierTemp) {
   boolean result = true;
   try {
	 byte[] buffer = new byte[512];
	 FileOutputStream fos = openFileOutput(nomFichierTemp, MODE_WORLD_READABLE);
	 InputStream is = getAssets().open(nomFichierAsset);
	 int bytesRead = is.read(buffer);
	 while (bytesRead > 0) {
	   fos.write(buffer, 0, bytesRead);
	   bytesRead = is.read(buffer);
	 }
	 fos.close();
	 is.close();
   } catch (FileNotFoundException e) {
	 // Cas d'erreur de création de fichier
	 Log.d("xx", "Erreur creation fichier ", e);
	 result = false;
   } catch (IOException e) {
	 // Cas d'erreur de lecture de fichier
	 Log.d("xx", "Erreur lecture fichier", e);
	 result = false;
   }
   return result;
 }  
// POUR LE BOUTON
 public void onClick(View src) {
 Intent i = new Intent(this, TabAndroidActivity.class);
 startActivity(i);
 };
 }

j'ai essayé ça :

		   maListViewPerso25.setOnItemClickListener(new OnItemClickListener() {
			   public void onItemClick(AdapterView<?> a, View v, int position, long id) {
					 HashMap<String, String> map = (HashMap<String, String>) maListViewPerso25.getItemAtPosition(position);
 if(map.get("titre").equals("3 - CBP sur Facebook")) {
	startActivity(new Intent(Tab5.this, TabAndroidActivity.class));
	return;
}
//SUITE ....

Voila résolu tout seul en cherchant ;)

Modifié par gogui63
  • 1 year later...
Posté(e)

Je ne vais pas apporter de réponse à votre question étant donné que vous l'avez déjà trouvé, simplement vous remercier de m'avoir dépanner puisque j'avais oublié la ligne :

HashMap map = (HashMap) maListViewPerso.getItemAtPosition(position);

Merci encore, mes cheveux vous en remercient également, vous leur avez sauvé la vie (ils sont passés à deux doigts de l'arrachage sauvage).

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

×   Collé en tant que texte enrichi.   Coller en tant que texte brut à la place

  Seulement 75 émoticônes maximum sont autorisées.

×   Votre lien a été automatiquement intégré.   Afficher plutôt comme un lien

×   Votre contenu précédent a été rétabli.   Vider l’éditeur

×   Vous ne pouvez pas directement coller des images. Envoyez-les depuis votre ordinateur ou insérez-les depuis une URL.

×
×
  • Créer...