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 :

utilisation des intents en android


Sujet :

Android

  1. #1
    Membre du Club
    Homme Profil pro
    Etudiant
    Inscrit en
    Avril 2016
    Messages
    60
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Vienne (Limousin)

    Informations professionnelles :
    Activité : Etudiant
    Secteur : Biens de consommation

    Informations forums :
    Inscription : Avril 2016
    Messages : 60
    Points : 58
    Points
    58
    Par défaut utilisation des intents en android
    Bonjour, j'ai des difficultés à gérer deux activity en android. je veux faire une recherche dynamique par le nom dans une base de donée, je veux saisir dans l'edittext de la premiere acticity qui est la suivante:
    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
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
     
            android:id="@+id/edittext"
            android:layout_width="299dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="42dp"
            android:label="Nom :" />
     
        <Button
            android:id="@+id/recherch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rechercher"
            android:layout_below="@+id/edittext"
            android:layout_alignEnd="@+id/edittext"
            android:layout_alignParentStart="true" />
     
    </RelativeLayout>
    la classe java associée est:
    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
    public class Recherche extends Activity {
        EditText editTex;
        String value;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recherche);
            Button buton= (Button) findViewById(R.id.recherch) ;
             editTex=(EditText) findViewById(R.id.edittext) ;
             value=String.valueOf(editTex.getText());
            buton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   Intent intent=new Intent(Recherche.this, MainActivity.class);
                    intent.putExtra("edit",value);
                    startActivity(intent);
                    finish();
                }
            });
    }
    }
    et recuperer dans la la seconde activité qui a pour classe java associée
    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
    package com.example.ouste4863.jsonsuite;
     
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.EditText;
    import android.widget.TextView;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONObject;
     
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
     
    public class MainActivity extends AppCompatActivity {
        /*int id= (int) jArray.getJSONObject();
        String nom= (String) jArray.get(2);
        String type= (String) jArray.get(3);
        double prix= (double) jArray.get(4);*/
        TextView ident= null;
        TextView name= null;
        TextView types= null;
        TextView pri= null;
        EditText editext;
         static String marque;
     
       // ident.setText(String.valueOf(id));name.setText(nom);types.setText(type);pri.setText(String.valueOf(prix));*/
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            types= (TextView) findViewById(R.id.type);
            name=(TextView) findViewById(R.id.nom);
            pri=(TextView) findViewById(R.id.Prix);
            ident=(TextView) findViewById(R.id.identifiant);
            Intent inte =getIntent();
            marque=inte.getStringExtra("edit");
            new Charge().execute();
        }
     
        public class Charge extends AsyncTask<String, String, String>{
     
     
            InputStream is = null; String result = ""; String error_text=""; JSONObject j = null;
     
     
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
     
            }
     
            @Override
            protected  String doInBackground(String... urls) {
                try {
     
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                   // nameValuePairs.add(new BasicNameValuePair("name","Apple"));
                    nameValuePairs.add(new BasicNameValuePair("name",marque));
                    // Envoie de la commande http
                    HttpResponse response=null;
                    try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://192.168.43.218/android/fichier.php");
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
     
                        //InputStream input = new BufferedInputStream(url.openStream(), 8192);
                        while ((line = reader.readLine()) != null) {
     
     
     
                            sb.append(line + "\n");
                            //while ((count = input.read(data)) != -1) {
     
                        }
                        is.close();
                        result=sb.toString();
     
     
                    }catch(Exception e){
                        Log.e("log_tag", "Error in http connection " + e);
                    }
     
     
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return result;
            }
     
            @Override
            protected void onPostExecute(String result) {
                try{
                    String ch="";
                    //dismissDialog(progress_bar_type);
                    Log.e("log_tag 1", "retourn Affiche  " + result );
                    if(!result.equals("")) {
                        JSONArray jArray = new JSONArray(result);
                        for (int i = 0; i < jArray.length(); i++) {
     
                            JSONObject json_data = jArray.getJSONObject(i);
                            Log.i("log_tag 2","id: "+json_data.getString("ID")+
                                    ", name: "+json_data.getString("NAME")+
                                    ", type: "+json_data.getString("TYPE")+
                                    ", price: "+json_data.getString("PRICE")
     
                            );
                            types.setText( " type: "+json_data.getString("TYPE"));
                            pri.setText("price: "+json_data.getString("PRICE"));
                            ident.setText("id: "+json_data.getString("ID"));
                            name.setText( "name: "+json_data.getString("NAME"));
     
                        }
     
                    }
                }catch(Exception e){
     
     
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }
     
            }
     
     
        }
     
     
     
    }
    je parviens à faire le run mais l'appli s'arrete dès que je clique sur le bouton de l'activity recherche je ne sais pas si c'est qui ne marche pas.....aidez moi svp

  2. #2
    Modérateur
    Avatar de grunk
    Homme Profil pro
    Lead dév - Architecte
    Inscrit en
    Août 2003
    Messages
    6 691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Lead dév - Architecte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2003
    Messages : 6 691
    Points : 20 222
    Points
    20 222
    Par défaut
    Tu as sans doute un message d'erreur dans le logcat.
    Commence par là ...
    Pry Framework php5 | N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Utiliser des drivers linux avec Android
    Par matleb dans le forum Android
    Réponses: 4
    Dernier message: 01/02/2011, 17h12
  2. Utiliser des drivers linux avec android
    Par matleb dans le forum Android
    Réponses: 3
    Dernier message: 23/11/2010, 11h29

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