Jump to content

Lancer une nouvelle activity depuis ma ListView


gogui63

Recommended Posts

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 ;)

Edited by gogui63
Link to comment
Share on other sites

  • 1 year later...

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).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...