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 :

Récupérer données lors d'un onListItemClick


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de janyoura
    Femme Profil pro
    étudiante ingénierie informatique
    Inscrit en
    Mars 2012
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : étudiante ingénierie informatique

    Informations forums :
    Inscription : Mars 2012
    Messages : 365
    Par défaut Récupérer données lors d'un onListItemClick
    Bonjour,
    je récupère les données à partir d'une base de données distantes (serveur distant) et je n'arrive pas à savoir comment récupérer l'id d'un item lors du click sur ce dernier pour ensuite l'utiliser pour récupérer d'autre information sur cet item (numéro téléphone, mail).
    Pouvez vous m'aider? merci

    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
    public class GetChoiceActivity extends ListActivity implements OnClickListener {
    	String city;
    	String field;
     
        // Progress Dialog
        private ProgressDialog pDialog;
     
        // Creating JSON Parser object
        JSONParser jParser = new JSONParser();
     
        List<HashMap<String, String>> centersList;
     
        // url to get all products list
        private static String url_all_products = "http://lien.php";
     // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_PRODUCTS = "centers";
        private static final String TAG_PID = "_id";
        private static final String TAG_NAME = "name";
        private static final String TAG_TEL = "tel";
        private static final String TAG_ADDRESS = "address";
        private static final String TAG_FIELD = "field";
        private static final String TAG_EMAIL = "email";
        private static final String TAG_CITY = "city";
        ArrayList<String> list;
     
        // products JSONArray
        JSONArray centers = null;
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.resultat_list);
     
            Intent i = getIntent();
    	     city = i.getStringExtra(ResearchActivity.CITY);
    	     field = i.getStringExtra(ResearchActivity.FIELD);
    	     Toast.makeText(GetChoiceActivity.this, "city is: " + city + " field is: " + field, Toast.LENGTH_LONG).show();
            // Hashmap for ListView
            centersList = new ArrayList<HashMap<String, String>>();
     
            registerForContextMenu(getListView());
            // Loading products in Background Thread
            new LoadAllCenters().execute();
     protected String doInBackground(String... args) {
                // Building Parameters
            	 Log.d("detail choice ", "city is: " + city + " field is: " + field);
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("city", city));
                params.add(new BasicNameValuePair("field", field));
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
     
                // Check your log cat for JSON reponse
               Log.d("Centers with selected options are: ", json.toString());
     
                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(TAG_SUCCESS);
     
                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                        centers = json.getJSONArray(TAG_PRODUCTS);
     
                        // looping through All Products
                        for (int i = 0; i < centers.length(); i++) {
                            JSONObject c = centers.getJSONObject(i);
     
                            // Storing each json item in variable
                            String id = c.getString(TAG_PID);
                            String name = c.getString(TAG_NAME);
                            String tel = c.getString(TAG_TEL);
                            String address = c.getString(TAG_ADDRESS);
                            String field = c.getString(TAG_FIELD);
                            String city = c.getString(TAG_CITY);
                            String email = c.getString(TAG_EMAIL);
     
                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();
     
                            // adding each child node to HashMap key => value
                            map.put(TAG_PID, id);
                            map.put(TAG_NAME, name);
                            map.put(TAG_TEL, tel);
                            map.put(TAG_ADDRESS, address);
                            map.put(TAG_CITY, city);
                            map.put(TAG_FIELD, field);
                            map.put(TAG_EMAIL, email);
     
                            // adding HashList to ArrayList
                            centersList.add(map);
                            list = new ArrayList<String>(map.values());
                        }
                    } 
                } catch (JSONException e) {
                    e.printStackTrace();
                }
     
                return null;
            }
     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 into ListView
                         * */
                        ListAdapter adapter = new SimpleAdapter(
                        		GetChoiceActivity.this, centersList,
                                R.layout.list_item, new String[] {
                                        TAG_NAME, TAG_TEL, TAG_EMAIL, TAG_ADDRESS, TAG_CITY, TAG_FIELD},
                                new int[] { R.id.textName,R.id.TextTel,R.id.TextEmail,R.id.TextAddress,R.id.TextCity,R.id.TextField });
                        // updating listview
                        setListAdapter(adapter);
                    }
                });
     
            }
    @Override // Selection d'un item de la liste
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		//comment récupere l'id de l'item
    		v.showContextMenu();

  2. #2
    Expert confirmé

    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
    Par défaut
    Bonjour,

    This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.

    Parameters
    l The ListView where the click happened
    v The view that was clicked within the ListView
    position The position of the view in the list
    id The row id of the item that was clicked
    id : identifiant de la ligne.
    position : position dans ta liste.

    Si c'est l'id TAG_PID que tu cherches alors il faut que tu récupères l'éléments avec la position puis depuis l'élément récupéré faire un getter sur son _id.

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

Discussions similaires

  1. [MySQL] Récupérer les données lors d'une requete
    Par gguiz dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 03/11/2006, 13h34
  2. récupérer données de plusieurs checkbox
    Par themis121 dans le forum Langage
    Réponses: 46
    Dernier message: 28/11/2005, 16h30
  3. [JTable] Récupérer données d'une cellule éditée
    Par lilou77 dans le forum Composants
    Réponses: 9
    Dernier message: 14/09/2005, 10h34
  4. [Servlet] Méthode Get : récupérer données de l'url
    Par david71 dans le forum Servlets/JSP
    Réponses: 9
    Dernier message: 01/02/2005, 11h52
  5. [Fichier] Récupérer donnée d'un fichier
    Par johnlehardos dans le forum Entrée/Sortie
    Réponses: 8
    Dernier message: 11/05/2004, 13h42

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