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

Composants graphiques Android Discussion :

Affichage ListView via MySQL


Sujet :

Composants graphiques Android

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2012
    Messages
    43
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

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

    Informations forums :
    Inscription : Décembre 2012
    Messages : 43
    Points : 29
    Points
    29
    Par défaut Affichage ListView via MySQL
    Salut a tous,

    Voila j'essaye depuis quelque jours d'afficher via une listview les informations qui sont dans ma base de données, mais lorsque je clique sur mon bouton pour afficher les informations de ma listview elle est vide, alors que le code semble être bon enfin techniquement.

    Donc en 3 étapes mon logcat ensuite mon code java et xml

    Étape 1:
    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
     
    11-28 19:47:46.392: D/Tous les contacts:(844): {"success":1,"contact":[{"prenom": ... etccc...}]}
     
    11-28 19:47:46.433: I/Choreographer(844): Skipped 41 frames!  The application may be doing too much work on its main thread.
     
    11-28 19:47:46.492: W/System.err(844): org.json.JSONException: No value for contacts
    11-28 19:47:46.562: W/System.err(844): 	at org.json.JSONObject.get(JSONObject.java:354)
     
     
    11-28 19:47:46.592: I/Choreographer(844): Skipped 40 frames!  The application may be doing too much work on its main thread.
     
     
    11-28 19:47:46.712: W/System.err(844): 	at org.json.JSONObject.getJSONArray(JSONObject.java:548)
     
    11-28 19:47:47.042: W/System.err(844): 	at fr.paris8.contactcloud.ListeContactActivity$LoadAllContacts.doInBackground(ListeContactActivity.java:142)
    11-28 19:47:47.112: W/System.err(844): 	at fr.paris8.contactcloud.ListeContactActivity$LoadAllContacts.doInBackground(ListeContactActivity.java:1)
     
     
     
    11-28 19:47:47.633: I/Choreographer(844): Skipped 31 frames!  The application may be doing too much work on its main thread.
     
     
    11-28 19:47:48.342: W/System.err(844): 	at android.os.AsyncTask$2.call(AsyncTask.java:287)
     
     
    11-28 19:47:48.382: I/Choreographer(844): Skipped 52 frames!  The application may be doing too much work on its main thread.
     
     
    11-28 19:47:48.452: W/System.err(844): 	at java.util.concurrent.FutureTask.run(FutureTask.java:234)
     
     
    11-28 19:47:48.563: W/System.err(844): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
     
     
    11-28 19:47:48.654: W/System.err(844): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    11-28 19:47:48.722: W/System.err(844): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
     
     
    11-28 19:47:48.752: I/Choreographer(844): Skipped 37 frames!  The application may be doing too much work on its main thread.
    11-28 19:47:48.823: W/System.err(844): 	at java.lang.Thread.run(Thread.java:841)
    Étape 2:
    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
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    import org.apache.http.NameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
     
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
     
    public class ListeContactActivity extends ListActivity{
     
    	// Pop-up Chargement
        private ProgressDialog pDialog;
     
        // Creating JSON Parser object
        JSONParser jParser = new JSONParser();
     
        ArrayList<HashMap<String, String>> contactList;
     
    	// url to get all contacts list
    	private static String url_all_contacts = "http://10.0.2.2/contactCloud/list_contact.php";//10.0.2.2
     
        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_CONTACT = "contacts";
        private static final String TAG_IDCONTACT = "idCONTACT";
        private static final String TAG_NOM = "nom";
     
        // contacts JSONArray
        JSONArray contacts = null;
     
        ///////////////////////////////////////////////////////////////////////////////////////
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_contact);
            //setContentView(R.layout.list_item);
     
            // Hashmap for ListView
            contactList = new ArrayList<HashMap<String, String>>();
     
            // Loading contacts in Background Thread
            new LoadAllContacts().execute();
     
            // Get listview
            ListView lv = getListView();
     
     
            lv.setOnItemClickListener(new OnItemClickListener() {
     
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                		int position, long id) {
                    // getting values from selected ListItem
                    String idCONTACT = ((TextView) view.findViewById(R.id.idCONTACT)).getText()
                    		.toString();
     
                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(),
                    		MajContactActivity.class); 
                    // sending pid to next activity
                    in.putExtra(TAG_IDCONTACT, idCONTACT);
     
                    // starting new activity and expecting some response back
                    startActivityForResult(in, 100);
                }
            });
        }
     
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                // if result code 100
                if (resultCode == 100) {
                    // if result code 100 is received
                    // means user edited/deleted product
                    // reload this screen again
                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }
            }
     
     
            class LoadAllContacts extends AsyncTask<String, String, String> {
     
                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(ListeContactActivity.this);
                    pDialog.setMessage("Chargement des contacts. Patienter...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(false);
                    pDialog.show();
                }
     
     
                /**
                 * getting All contacts from url
                 * */
                protected String doInBackground(String... args) {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    // getting JSON string from URL
                    JSONObject json = jParser.makeHttpRequest(url_all_contacts, "GET", params);
     
                    // Check your log cat for JSON reponse
                    Log.d("Tous les contacts: ", json.toString());
     
                    try {
                        // Checking for SUCCESS TAG
                        int success = json.getInt(TAG_SUCCESS);
     
                        if (success == 1) {
                            // contacts found
     
                            contacts = json.getJSONArray(TAG_CONTACT);
     
                            for (int i = 0; i < contacts.length(); i++) {
                                JSONObject c = contacts.getJSONObject(i);
     
                                // Storing each json item in variable
                                String id = c.getString(TAG_IDCONTACT);
                                String nom = c.getString(TAG_NOM);
     
                                // creating new HashMap
                                HashMap<String, String> map = new HashMap<String, String>();
     
                                // adding each child node to HashMap key => value
                                map.put(TAG_IDCONTACT, id);
                                map.put(TAG_NOM, nom);
     
                                // adding HashList to ArrayList
                                contactList.add(map);
                            }
                        } else {
     
                            Intent i = new Intent(getApplicationContext(),
                            		AjoutContactActivity.class);
                            // Closing all previous activities
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
     
                    return null;
                }
     
                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog after getting all contacts
                    pDialog.dismiss();
                    // updating UI from Background Thread
                    runOnUiThread(new Runnable() {
                        public void run() {
                            /**
                             * Updating parsed JSON data into ListView
                             * */
                            ListAdapter adapter = new SimpleAdapter(
                            		ListeContactActivity.this, contactList, 
                            		R.layout.list_item, new String[] { TAG_IDCONTACT, 
                            				TAG_NOM}, 
                            		new int[] { R.id.idCONTACT, R.id.nom });
                            // updating listview
                            setListAdapter(adapter);
                        }
                    });
     
                }
     
            }
        }
    Étape 3:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    list_contact.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
     
        <!-- Principal ListView	-->
    	<ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
     
    </LinearLayout>
    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
     
    list_item.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
     
     
        <TextView
            android:id="@+id/idCONTACT"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />
     
        <!-- Nom du contact -->
        <TextView
            android:id="@+id/nom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:textSize="17dip"
            android:textStyle="bold" />
     
     
    </LinearLayout>

  2. #2
    Rédacteur
    Avatar de David55
    Homme Profil pro
    Ingénieur informatique
    Inscrit en
    Août 2010
    Messages
    1 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2010
    Messages : 1 542
    Points : 2 808
    Points
    2 808
    Par défaut
    Bonjour,

    Modifies ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     private static final String TAG_CONTACT = "contacts";
    Par ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     private static final String TAG_CONTACT = "contact";

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2012
    Messages
    43
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

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

    Informations forums :
    Inscription : Décembre 2012
    Messages : 43
    Points : 29
    Points
    29
    Par défaut
    Merci honnêtement, je n'aurais jamais cru que sa serais a cause de la variable contacts par contact mais j'aimerais savoir pourquoi sa ne marcher pas car la tu es arrivée et en deux ligne tu as résolu l'affaire.

  4. #4
    Rédacteur
    Avatar de David55
    Homme Profil pro
    Ingénieur informatique
    Inscrit en
    Août 2010
    Messages
    1 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2010
    Messages : 1 542
    Points : 2 808
    Points
    2 808
    Par défaut


    Car le nom de ton champ est "contact" et non "contacts":
    {"success":1,"contact":[{"prenom": ... etccc...}]}

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2014
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2014
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Pour un plus a cette disscussion, voici un tutoriel avec code source pour implementer une ListView via SQLite . Android SQLite Database Tutorial With Multiple Tables

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

Discussions similaires

  1. [MySQL] non affichage d'image sur ma page php via mysql
    Par cristelle1986 dans le forum PHP & Base de données
    Réponses: 12
    Dernier message: 02/09/2014, 21h17
  2. Mise à jour d'une ListView via MySQL
    Par pouti dans le forum Composants graphiques
    Réponses: 15
    Dernier message: 14/12/2013, 22h14
  3. Affichage complexe(?) PHP-MySQL
    Par Lypertrophie dans le forum Requêtes
    Réponses: 2
    Dernier message: 05/09/2006, 11h05
  4. [Ajax] pb avec 3 combos listes recupérant les infos via mysql
    Par laulaurent dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 31/05/2006, 18h38
  5. [API]PB Affichage ListView
    Par Taron31 dans le forum MFC
    Réponses: 3
    Dernier message: 26/03/2006, 00h43

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