user1422281
user1422281

Reputation: 11

Start Activity from a ListView

I am French but I'll try to explain my problem I have a ListView and in this ListView I'd like to start DIFFERENT activity, I explained : The first two Items must launch a PDF but the third " Sur Facebook" must to launch a new activity, specifically a new view (facebook.java), I can do it with a button but I can not since my ListView , here is my code :

    package com.androiddev.tab;

import java.io.FileNotFoundException;
...

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

Upvotes: 0

Views: 409

Answers (2)

AITAALI_ABDERRAHMANE
AITAALI_ABDERRAHMANE

Reputation: 2519

try this code:

if(map.get("titre").equals("YourTile") {
    Intent mainIntent = new Intent(Tab5.this, "Your Facebook Activity Here".class);
    startActivity(mainIntent);
}

Upvotes: 2

Alexander
Alexander

Reputation: 48262

You can do first thing in onItemClick

if(map.get("titre").equals("3 - Sur Facebook") {
    startActivity(new Intent(this, TabAndroidActivity.class));
    return;
}

This is ugly but if it is what you're looking for then you will be able to do it better

Upvotes: 1

Related Questions