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 :

Créer une ListView avec des radio button ?


Sujet :

Composants graphiques Android

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2013
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Janvier 2013
    Messages : 68
    Points : 35
    Points
    35
    Par défaut Créer une ListView avec des radio button ?
    j'ai crée un listview qui récupère des données d'une base de données MySQL
    maintenant je veux que mon listview crée s'affiche avec des radios buttons comment faire svp aidez moi et merci d'avance pour vos réponses voila mon code:
    code XML:
    Code xml : 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
     
    <?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"
        android:background="#000000">
     
        <TextView
    android:id="@+id/textSexe"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="#AEEE00"
    android:padding="25dip" />
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:choiceMode="singleChoice"
            />
     
    </LinearLayout>

    et voila mon code java:
    Code java : 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
     
    package com.example.base;
    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.ArrayAdapter;
    import android.widget.ListAdapter;
     
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
     
    public class AllProductsActivity extends ListActivity {
     
        private ProgressDialog pDialog;
     
        JSONParser jParser = new JSONParser();
     
        ArrayList<HashMap<String, String>> productsList;
     
        private static String url_all_products = "http://10.0.2.2/android_connect/distance.php";
     
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_PRODUCTS = "products";
        private static final String TAG_PID = "pid";
        private static final String TAG_NAME = "name";
        JSONArray products = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.all_products);
            productsList = new ArrayList<HashMap<String, String>>();
            ListView lv = getListView();
     
            new LoadAllProducts().execute();
     
     
     
            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
     
     
                    String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                            .toString();
     
                    Intent in = new Intent(getApplicationContext(),
                            EditProductActivity.class);
                    in.putExtra(TAG_PID, pid);
     
                    startActivityForResult(in, 100);
                }
            });
     
        }
     
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == 100) {
     
                Intent intent = getIntent();
                finish();
                startActivity(intent);
            }
     
        }
     
     
        class LoadAllProducts extends AsyncTask<String, String, String> {
     
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(AllProductsActivity.this);
                pDialog.setMessage("Loading products. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
     
            }
     
     
     
            protected String doInBackground(String... args) {
     
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
     
                Log.d("All Products: ", json.toString());
     
                try {
                    int success = json.getInt(TAG_SUCCESS);
     
                    if (success == 1) {
                        products = json.getJSONArray(TAG_PRODUCTS);
     
                        for (int i = 0; i < products.length(); i++) {
                            JSONObject c = products.getJSONObject(i);
     
                            String id = c.getString(TAG_PID);
                            String name = c.getString(TAG_NAME);
     
                            HashMap<String, String> map = new HashMap<String, String>();
     
                            map.put(TAG_PID, id);
                            map.put(TAG_NAME, name);
     
                            productsList.add(map);
     
                        }
     
                    } else {
                        Intent i = new Intent(getApplicationContext(),
                                NewProductActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
     
                return null;
            }
     
     
            protected void onPostExecute(String file_url) {
                pDialog.dismiss();
                runOnUiThread(new Runnable() {
                    public void run() {
     
                        ListAdapter adapter = new SimpleAdapter(
                                AllProductsActivity.this, productsList,
                                R.layout.list_item, new String[] { TAG_PID,
                                        TAG_NAME},
                                new int[] { R.id.pid, R.id.name });
                        setListAdapter(adapter);
                    }
                });
     
            }
     
        }
    }

  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
    Juste un truc:

    Le runOnUiThread dans le onPostExecute n'est pas utile... on est DEJA dans l'UI thread dans le onPostExecute, c'est tout l'interêt des AsyncTask !

    Par contre, pas sur que le startActivity(i); dans le doInBackground() fonctionne correctement....
    L'objet "productsList" est modifié dans un thread, mais déclaré (et probablement utilisé) dans un autre... danger !
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2013
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Janvier 2013
    Messages : 68
    Points : 35
    Points
    35
    Par défaut
    Merci pour votre remarque mais vous m'avez pas répondu à ma question

  4. #4
    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
    Quelle question ? Comment rajouter un RadioButton ?

    Difficile à dire vu qu'on n'a pas le code (à priori) de R.layout.list_item
    Sinon, en général on rajoute juste un RadioButton genre:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    <RadioButton
            android:id="@android:R.id/radio"
            android:layout_width="35dip"
            android:layout_height="wrap_content"
            android:paddingEnd="12dip"
            android:gravity="center_vertical"
            android:focusable="false"
            android:clickable="false"
        />
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2013
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Janvier 2013
    Messages : 68
    Points : 35
    Points
    35
    Par défaut
    Merci pour votre réponse et voila le code de R.layout.list_item
    Code xml : 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
     
    <?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/pid"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />
     
        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:textSize="17dip"
            android:textStyle="bold" />
     
    </LinearLayout>

  6. #6
    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
    UTILISEZ BALISE CODE !!!!

    Bon ben sinon, je crois avoir répondu alors.

    (au fait un textSize s'exprime en "sp" et non en "dp")
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2013
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Janvier 2013
    Messages : 68
    Points : 35
    Points
    35
    Par défaut
    je vois pas la réponse

Discussions similaires

  1. Réponses: 13
    Dernier message: 30/05/2012, 10h42
  2. Probleme avec des Radio Button
    Par beb30 dans le forum MFC
    Réponses: 7
    Dernier message: 15/05/2006, 23h46
  3. Créer une liste avec des noms de fichiers
    Par Jeffboj dans le forum Access
    Réponses: 5
    Dernier message: 12/05/2006, 05h48
  4. Créer une vue avec des requêtes UNION ?
    Par webtheque dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 04/04/2005, 12h37

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