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 :

Gestion clic sur bouton d'un item liste


Sujet :

Composants graphiques Android

  1. #1
    Membre actif 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
    Points : 279
    Points
    279
    Par défaut Gestion clic sur bouton d'un item liste
    Bonjour,
    Je n'arrive pas à inclure dans ma liste la gestion du clic sur le bouton de chaque item. Normalment lors du clic sur le bouton il y aura lancement d'une nouvelle activité en lui envoyant les paramètres de l'item sélectionné (id, tel, nom, etc)
    J'ai essayé de testé juste avec un toast dans la méthode onClick() mais ça ne fonctionne pas. ou devrai-je mettre alors la fonction onClick()?
    voilà ma classe:
    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
    public class GetChoiceActivity extends ListActivity implements OnClickListener {
     @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);
    	     country = i.getStringExtra(ResearchActivity.COUNTRY);
    	   //  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();
     
        }
    class LoadAllCenters extends AsyncTask<String, String, String> {
     
            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(GetChoiceActivity.this);
                pDialog.setMessage("Loading list of training centers. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
     
            /**
             * getting All products from url
             * */
            protected String doInBackground(String... args) {
                // Building Parameters
            	 Log.d("detail choice ","country is: " + country + "city is: " + city + " field is: " + field);
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("country", country));
                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 country = c.getString(TAG_COUNTRY);
                            String email = c.getString(TAG_EMAIL);
                            double longitud = c.getDouble(TAG_LONG);
                            String longitude = Double.toString(longitud);
                            double lati = c.getDouble(TAG_LAT);
                            String latitude = Double.toString(lati);
                            String link = c.getString(TAG_LINK);
     
                            // 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_COUNTRY, country);
                            map.put(TAG_FIELD, field);
                            map.put(TAG_EMAIL, email);
                            map.put(TAG_LONG, longitude);
                            map.put(TAG_LAT, latitude);
                            map.put(TAG_LINK, link);
     
     
                            // adding HashList to ArrayList
                            centersList.add(map);
                            list = new ArrayList<String>(map.values());
                        }
                    } 
                } 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 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.trackrow, new String[] {
                                        TAG_NAME},
                                new int[] { R.id.textName });
                        // updating listview
                        setListAdapter(adapter);
     
                    }
                });
     
            }
     
        }
       	@Override // Selection d'un item de la liste
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		Map<String,String> item =  (Map<String, String>) l.getItemAtPosition(position);
     
    		 _id = item.get(TAG_PID);
    		name = item.get(TAG_NAME);
    		 tel = item.get(TAG_TEL);
    		 email = item.get(TAG_EMAIL);
    		address = item.get(TAG_ADDRESS);
    		String longitud = item.get(TAG_LONG);
    		 longitude = Double.parseDouble(longitud);
    		String latitud = item.get(TAG_LAT);
    		 latitude = Double.parseDouble(latitud);
    		 link = item.get(TAG_LINK);
    		 Intent details = new Intent(GetChoiceActivity.this, OptionsMenu.class);
    			details.putExtra(ID, _id);
    			details.putExtra(ADDRESS, address);
    			details.putExtra(EMAIL, email);
    			details.putExtra(LINK, link);
    			details.putExtra(LONG, longitude);
    			details.putExtra(LAT, latitude);
    			details.putExtra(NAME, name);
    			details.putExtra(TEL, tel);
    			details.putExtra(LINK, link);
    			startActivity(details);	
     
     
    		//Intent details = new Intent(GetChoiceActivity.this, DetailActivity.class);
    		//details.putExtra(ID, _id);
    		//startActivity(details);	
    		//Toast.makeText(GetChoiceActivity.this, "l'identifiant est: " + _id , Toast.LENGTH_LONG).show();
     
    		//v.showContextMenu();
     
    	}
    @Override
    	public void onClick(View arg0) {
    		// TODO Auto-generated method stub
    		Toast.makeText(GetChoiceActivity.this, "test clic "  , Toast.LENGTH_LONG).show();
     
    	}
    }
    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="@drawable/lancement2">
     
     
         <ListView
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:id="@android:id/list"
        />
     
    </LinearLayout>
    Liste:
    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
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:orientation="horizontal" android:layout_width="fill_parent"
    	android:layout_height="fill_parent" android:id="@+id/background"
    	>
     
    	<RelativeLayout android:layout_centerVertical="true"
    		android:paddingLeft="4dp" android:layout_width="wrap_content"
    		android:layout_height="wrap_content">
     
    		<TextView android:id="@+id/textName"
    			android:layout_width="wrap_content" android:layout_height="wrap_content"
    			android:textStyle="bold" 
    			android:shadowColor="@color/black" android:shadowRadius="0.5"
    			android:shadowDx="0" android:shadowDy="0"  android:layout_marginLeft="4dip"></TextView>
     
    	</RelativeLayout>
     
    	<Button android:id="@+id/track_button"
    		android:text="Details" android:layout_alignParentRight="true" android:layout_centerVertical="true"
    		android:layout_width="wrap_content" android:layout_height="fill_parent"></Button>
     
     
     
    </RelativeLayout>
    Pouvez vous m'aider?
    Merci
    "Scientists dream about doing great things. Engineers do them.”

    La réussite après tant de travail est un sentiment à vivre

    Si ton message est résolu, il y a un bouton qui est fait pour ça :
    Il se trouve tout en bas de la conversation !

    N'oublie pas que si ce message t'as aidé, tu peux voter pour lui en utilisant

  2. #2
    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
    le runOnUIThread() dans le onPostExecute() est de trop... on est déjà dans le UI thread (c'est tout l'interêt des AsyncTask).

    Sinon... il n'est pas recommandé de mettre un bouton dans une list-view mais si c'est vraiment nécessaire, il faut que le bouton soit capable de référencer la position dans la liste...
    Par exemple avec un tag. (btn.setTag(position)

    Dans tous les cas, il faudra se séparer du SimpleAdapter (qui, comme son nom l'indique, ne gère que les cas simples).
    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: 5
    Dernier message: 15/12/2009, 19h47
  2. [VB.NET] Trouver un handle, simuler clic sur bouton
    Par 6su7 dans le forum Windows Forms
    Réponses: 33
    Dernier message: 21/08/2006, 09h38
  3. charger nouvelle page par clic sur bouton (pas pop-up)
    Par michaelbob dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 12/09/2005, 16h04
  4. Detection clic sur bouton precedent
    Par shaun_the_sheep dans le forum Servlets/JSP
    Réponses: 4
    Dernier message: 20/10/2004, 17h18
  5. Réponses: 9
    Dernier message: 23/02/2004, 19h14

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