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 :

Question Java [Débutant(e)]


Sujet :

Android

  1. #1
    Candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut Question Java
    Bonjour,

    voici mon code :

    Code Java :
    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
     
    public class FollowActivity extends GDListActivity {
     
        // Progress Dialog
        private ProgressDialog pDialog;
     
        // Creating JSON Parser object
        JSONParser jParser = new JSONParser();
     
        ArrayList<HashMap<String, String>> pictureList;
     
        // url to get all products list
        private static String url_all_picture = "http://10.0.2.2/android_connect/getall.php";
     
        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_PICTURE = "picture";
        private static final String TAG_PICTURES = "pictures";
        private static final String TAG_COMMENT = "comment";
        private static final String TAG_DATE = "date";
     
     
        // products JSONArray
        JSONArray pictures = null;
     
     
        private final Handler mHandler = new Handler();
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
            getActionBar().setType(greendroid.widget.ActionBar.Type.Empty);
            setTitle(R.string.app_name);
     
            List<Item> items = new ArrayList<Item>();
     
            //ici je dois remplir des plus récente photo
     
            // Hashmap for ListView
            pictureList = new ArrayList<HashMap<String, String>>();
     
            // Loading products in Background Thread
     
            // new LoadAllProducts(ici genre !!!).execute();


    dans le fond je voudrais envoyer le code suivant en background plus bas
    mais étant donnée que mon ItemAdapter plus bas vient de greendroid
    je ne comprend pas trop comment faire mais il y a surment moyen de l'envoyer
    a ma class LoadAllProducts

    Code Java :
    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
     
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
     
     
     
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_picture, "GET", params);
     
            if (json==null) {
                Toast.makeText(this,
                "json==null",
                Toast.LENGTH_SHORT).show();
                return;
            }
     
            // Check your log cat for JSON reponse
              Log.d("All Products: ", json.toString());
     
     
     
            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);
     
                if (success == 1) {
                    // products found
                    // Getting Array of Products
                	pictures = json.getJSONArray(TAG_PICTURES);
     
                    // looping through All Products
                    for (int i = 0; i < pictures.length(); i++) {
                        JSONObject c = pictures.getJSONObject(i);
     
                        // Storing each json item in variable
                        String id = c.getString(TAG_PICTURE);
                        String name = c.getString(TAG_COMMENT);
                        String date = c.getString(TAG_DATE);
     
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
     
                        // adding each child node to HashMap key => value
                        map.put(TAG_PICTURE, id);
                        map.put(TAG_COMMENT, name);
                        map.put(TAG_DATE, date);
     
                        // adding HashList to ArrayList
                        pictureList.add(map);
                    }
                } else {
     
     
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
     
     
            items.add(new SeparatorItem(TAG_DATE));
            items.add(new ThumbnailItem("Powered paragliding", "aka paramotoring", R.drawable.gd_action_bar_help));
            items.add(new DescriptionItem(TAG_COMMENT));
     
            items.add(new SeparatorItem("Class 2"));
            items.add(new DrawableItem("Trikes", R.drawable.gd_action_bar_help));
            items.add(new DescriptionItem("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in."));
     
            items.add(new SeparatorItem("Class 3"));
            items.add(new ThumbnailItem("Multi-axis", "Looks like a tiny plane", R.drawable.gd_action_bar_help));
            items.add(new DescriptionItem("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in."));
     
            items.add(new SeparatorItem("Class 4"));
            items.add(new ThumbnailItem("Auto-gyro", "A scary helicopter", R.drawable.gd_action_bar_help));
            items.add(new DescriptionItem("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in."));
     
            items.add(new SeparatorItem("Class 5"));
            items.add(new DrawableItem("Hot air baloon", R.drawable.gd_action_bar_help));
            items.add(new DescriptionItem("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in."));
     
            final Item item1 = new SeparatorItem("Class 6");
            final Item item2 = new TextItem("Airbus/Boeing planes");
            final Item item3 = new DescriptionItem("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus consequat leo, et tincidunt justo tristique in.");
            items.add(item1);
            items.add(item2);
            items.add(item3);
     
            final ProgressItem progressItem = new ProgressItem("Next !", true);
            items.add(progressItem);
     
            final ItemAdapter adapter = new ItemAdapter(this, items);
            setListAdapter(adapter);
     
            mHandler.postDelayed(new Runnable() {
                public void run() {
     
                    adapter.notifyDataSetChanged();
                }
            },8000);
        }
     
     
     
        /**
         * Background Async Task to Load all product by making HTTP Request
         * */
        class LoadAllProducts extends AsyncTask<String, String, String> {
     
            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(FollowActivity.this);
                pDialog.setMessage("Loading... Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
     
            /**
             * getting All products from url
             * */
            protected String doInBackground(String... args) {
     
     
                return null;
            }
     
            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data 
                         * */
     
     
     
     
                    }
                });
     
            }
     
        }
     
    }

    auriez-vous soit un site web a me proposé pour apprendre ceci ou pouvez-vous m'expliquer comment faire ?

    merci d'avance

  2. #2
    Candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    pour être plus clair.

    le problème est avec mon ItemAdapter j'aimerais l'utilisé dans la classe


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    class LoadAllProducts extends AsyncTask

    mais l'ItemAdapter vien de la class GDListActivity


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public class FollowActivity extends GDListActivity

    je crois que je pourrais l'envoyer dans mon


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new LoadAllProducts().execute()

    mais je dois faire des erreur de syntax ou quelque chose du genre
    je voudrais remplir mon UI ici


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
            protected String doInBackground(String... args) {
     
     
                return null;
            }

  3. #3
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    1 / Tu peux toujours passer les informations nécessaires à l'asyntask dans le constructeur ou via un Init avant d’appeler le execute().

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class LoadAllProducts extends AsyncTask<Void, Void, Void> {
        private ArrayList<HashMap<String, String>> mItems;
     
        public LoadAllProducts(ArrayList<HashMap<String, String>> items) {
            super();
            mItems = items;
        }
     
        // doInBackground() et al.
         protected String doInBackground(String... args) {
                 // use mItems ;
                 // do stuff
            }
    }
    L'appel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new LoadAllProducts(pictureList).execute();
    2 / Sinon tu peux utiliser les paramètres du execute

    // AsyncTask :< doInBackGround, onProgressUpdate, onPostExecute>

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    class LoadAllProducts extends AsyncTask<ArrayList<HashMap<String, String>>, Void, Void> {
     
        // doInBackground() et al.
         protected String doInBackground(ArrayList<HashMap<String, String>>) {
                 // use entries
            }
    }
    L'appel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new LoadAllProducts().execute(pictureList);
    Tout dépends de ce que tu veux faire exactement, normalement la solution 2 suffit .

    http://developer.android.com/referen...AsyncTask.html
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

Discussions similaires

  1. Question Java + socket
    Par elotro dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 10/08/2011, 15h07
  2. Réponses: 1
    Dernier message: 29/03/2011, 19h26
  3. question JAVA - UML
    Par oprian dans le forum UML
    Réponses: 2
    Dernier message: 01/06/2007, 12h56

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