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

Android Discussion :

Appel d'une Classe dans une autre Activité


Sujet :

Android

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 37
    Points : 33
    Points
    33
    Par défaut Appel d'une Classe dans une autre Activité
    Bonjour

    Je suis nouveau dans le developpement android . J'ai une activity MonEspace qui contient une autre class "ListAllFiles"
    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
    class ListAllFiles extends AsyncTask<Object, Object, Object> {
     
    		protected void onPostExecute(Object result) {
    			// TODO Auto-generated method stub
    			super.onPostExecute(result);
     
    			mListView.setEnabled(true);
    			mProgress.setVisibility(View.GONE);
    			removeDialog(0);
    			Log.i("MonEspace", "doInBack============End");
    		}
     
    		@Override
    		protected Object doInBackground(Object... params) {
    			// TODO Auto-generated method stub
     
    			Log.i("MonEspace", "doInBack============Start");
     
    			runOnUiThread(new Runnable() {
     
     
    				public void run() {
    					// TODO Auto-generated method stub
     
    					if (isOnline()) {
    						String textList = null;
    						MultiStatus ms = null;
     
    						try {
    							ms = listAll(url, httpClient);
    							String[] name = url.split("webdav.php/");
     
    							mCurrentDir.setText(" Cloud/"+URLDecoder.decode(name[1]));
     
    						} catch (Exception e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    							mCurrentDir.setText(" Cloud/");
    						}
    						// if (ms.equals(null)) {
    						if (ms == null) {
    							// Toast.makeText(getApplicationContext(),
    							// "There is some problem in listing files",
    							// 1).show();
    						} else {
    							listUrl = new String[ms.getResponses().length - 1];
     
    							listName = new String[ms.getResponses().length - 1];
    							Log.e("length",
    									String.valueOf(ms.getResponses().length));
    							if (ms.getResponses().length == 1) {
    								mTextFlag=false;
    								Toast.makeText(
    										getApplicationContext(),
    										"Le dossier est vide.Cliquer sur le bouton retour",
    										1).show();
     
    								mNoText.setVisibility(View.INVISIBLE);
    							} else {
    								mTextFlag=false;
    								//mNoText.setVisibility(View.GONE);
    								for (int i = 1; i <= ms.getResponses().length - 1; i++) {
    									String respString = ms.getResponses()[i]
    											.getHref();
    									Log.v(TAG,"Response"+respString);
    									listUrl[i - 1] = ms.getResponses()[i]
    											.getHref();
    									File file = new File(URLDecoder.decode(ms
    											.getResponses()[i].getHref()));
    									mListFile.add(file.getName());
    									Log.i("ListFile", String.valueOf(i) + "  "
    											+ file.getName());
     
    									String fileName = new StringBuffer(
    											listUrl[i - 1]).reverse()
    											.toString();
    									String[] fileNameArray = fileName
    											.split("/");
    									if ((fileName.substring(0, 1)).equals("/")) {
    										// fileName = fileName.substring(1);
    										listName[i - 1] = new StringBuffer(
    												fileNameArray[1] + "/")
    												.reverse().toString();
     
     
     
     
    									} else {
     
    										listName[i - 1] = new StringBuffer(
    												fileNameArray[0]).reverse()
    												.toString();
    									}
    									if (textList == null) {
    										textList = listName[i - 1] + "\n";
    									} else {
    										textList = textList + listName[i - 1]
    												+ "\n";
    									}
     
    									Log.d("response", respString);
    								}
    							}
     
    							adapter.setData(listName);
    							mListView.setAdapter(adapter);
    						}
     
    					} else {
    						WebNetworkAlert();
    					}
    				}
    			});
    			return null;
    		}
     
    	}
    Maintenant je veux appeler cette classe dans une nouvelle activity Favoris plus précisement dans le code ou le actionId==ID_OPE
    ceci le code de la classe Favoris:
    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
    package com.example.cloud;
     
    import java.io.File;
    import java.net.URLDecoder;
    import java.util.List;
     
    import org.apache.jackrabbit.webdav.ordering.Position;
     
    import com.example.cloud.MonEspace.ListAllFiles;
     
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.Handler;
    import android.os.Message;
    import android.os.Messenger;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.PopupWindow;
    import android.widget.Toast;
    public class Favoris extends Activity{
    	 int NOTFICATION_ID = 198990;
    	static Notification notification;
     
     
    	private FavorisDataSource datasource;
    	 public ListView list1=null;
    	public int scrollpos=0;
    	 private static final int ID_DEL = 2;
    	 private static final int ID_OPE = 3;
    	 private static final int ID_DOWN = 1;
    	private static final String TAG = null;
    	String fileName;
     
    	String mFileUrl = "";
     
    public  boolean isDownload=false;
    static NotificationManager notificationManager = null;
     String titleText;
    String notiText;
    protected AsyncTask<Object, Object, Object>Laf;
    private ListAllFiles LAF;
     
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_favoris);
     
    	    list1 =(ListView) findViewById(R.id.listView1);
     
    	    datasource = new FavorisDataSource(this);
    	    datasource.open();
     
    	    List<Fav> values = datasource.getAllFavoris();
    	    final Object[] table = values.toArray();
    if (table.length==0){Toast.makeText(
    		getApplicationContext(),
    		"La liste des favoris est vide!Cliquer sur le bouton retour",
    		1).show();}else{
    	    // Use the SimpleCursorAdapter to show the
    	    // elements in a ListView
    	    ArrayAdapter<Fav> adapter = new ArrayAdapter<Fav>(this,
    	            android.R.layout.simple_list_item_1, values);
    	    list1.setAdapter(adapter);
     
      adapter.notifyDataSetChanged();
    		}
        final ActionItem downItem = new ActionItem(ID_DOWN, "Telécharger",
    			getResources().getDrawable(R.drawable.icon_telecharger));
     
    	final ActionItem delItem = new ActionItem(ID_DEL, "Supprimer",
    			getResources().getDrawable(R.drawable.icon_supprimer));
     
     
    	final ActionItem opeItem = new ActionItem(ID_OPE, "Ouvrir", getResources()
    			 .getDrawable(R.drawable.icon_ouvrir));
     
     
    	final QuickAction mQuickAction = new QuickAction(this);
    	final QuickActionFolder mQuickActionFolder = new QuickActionFolder(this);
    	mQuickAction.addActionItem(downItem);
    	mQuickAction.addActionItem(delItem);
        mQuickActionFolder.addActionItem(opeItem);
     
        mQuickActionFolder.addActionItem(delItem);
     
    	list1.setOnItemClickListener(new OnItemClickListener() {
     
    			public void onItemClick(AdapterView<?> parent, View view,
    					int position, long id) {
    				scrollpos=position;
    				if (!((table[position].toString().substring(table[position].toString()
    						.length() - 1)).equals("/"))) {
     
    					mQuickAction.show(view);
     
    				} 
     
     
    				else  if (((table[position].toString().substring(table[position].toString()
    						.length() - 1)).equals("/"))){
     
    					mQuickActionFolder.show(view);}
     
     
    			}
    		});
     
    	mQuickActionFolder.setOnActionItemClickListener(new QuickActionFolder.OnActionItemClickListener() {
     
     
     
     
     
     
     
    		public void onItemClick(QuickActionFolder quickActionFolder, int position,
    				int actionId) {
    			Fav favoris = null;
    			ArrayAdapter<Fav> adapter = (ArrayAdapter<Fav>)list1.getAdapter();
    			ActionItem actionItem = quickActionFolder.getActionItem(position);
    			if (actionId == ID_DEL) {
    				if (list1.getAdapter().getCount() > 0) {
    			        favoris = (Fav) list1.getAdapter().getItem(scrollpos);
    			        datasource.deleteFavoris(favoris);
    			        adapter.remove(favoris);}
    				else if (actionId==ID_OPE){
     
     
    				}
     
    			}
    		}
     
    		public LayoutInflater getSystemService(String layoutInflaterService) {
     
    			return null;
    		}
    		});
    	mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
     
    		public void onItemClick(QuickAction quickAction, int pos, int actionId) {
    			Fav favoris = null;
    			ArrayAdapter<Fav> adapter = (ArrayAdapter<Fav>)list1.getAdapter();
    			ActionItem actionItem = quickAction.getActionItem(pos);
    			if (actionId == ID_DEL) {
    				if (list1.getAdapter().getCount() > 0) {
    			        favoris = (Fav) list1.getAdapter().getItem(scrollpos);
    			        datasource.deleteFavoris(favoris);
    			        adapter.remove(favoris);}
     
    		};
     
    				}
     
    		});
     
     
     
    	mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() {
    		public void onDismiss() {
     
    			// mMoreIv.setImageResource(R.drawable.ic_list_more);}
     
     
     
     
    				}
    			});
    	}
     
     
    	public boolean onOptionsItemSelected(MenuItem item) {
    		switch (item.getItemId()) {
    		case R.id.terminer:
    			finish();
     
    		case R.id.parametres:
    		{
     
    		}
     
    		}
    		return true; 
    		}
    	protected void onResume() {
    	    datasource.open();
    	    super.onResume();
    	  }
     
     
    	  protected void onPause() {
    	    datasource.close();
    	    super.onPause();
    	  }
     
     
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.favoris, menu);
    		return true;
    	}
     
    		}

  2. #2
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2011
    Messages
    115
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2011
    Messages : 115
    Points : 106
    Points
    106
    Par défaut
    Il te faut appeler la méthode execute de ta classe .
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    else if (actionId==ID_OPE){
    ListAllFiles   obj= new ListAllFiles ();
    obj.execute(tonObjectParametre); 
     				}

  3. #3
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    je ne vois pas l'interêt de faire un runOnUiThread(new Runnable() {
    dans le doInBackground


    On fait dans le doInBackground tout ce qui ne doit pas être fait par le thread UI.
    Et dans le onPostExecute() tout ce qui doit être fait dans le thread UI.

    D'autant que "listAll" va a mon avis faire un appel Internet, et donc crasher si executé dans le UI thread.
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

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

Discussions similaires

  1. Réponses: 8
    Dernier message: 05/07/2011, 12h51
  2. utilisation d'une variable d'une class dans une autre class
    Par the watcher dans le forum Langage
    Réponses: 7
    Dernier message: 31/08/2010, 15h01
  3. Eval d'une propriété d'une classe dans une classe
    Par bizet dans le forum ASP.NET
    Réponses: 4
    Dernier message: 28/10/2008, 10h43
  4. Détecter une action d'une classe dans une autre classe
    Par Pirokkk dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 12/05/2008, 13h14
  5. Réponses: 3
    Dernier message: 29/04/2008, 15h14

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