Android - Spinner y AsyncTask

 
Vista:
sin imagen de perfil
Val: 8
Ha disminuido 1 puesto en Android (en relación al último mes)
Gráfica de Android

Spinner y AsyncTask

Publicado por Jose (7 intervenciones) el 06/10/2015 19:32:18
Cordial saludo,

Solo llevo 15 dias incursionando en Android y Java, espero que no suene tonta la pregunta.

Mi aplicacion un EditText, un Button, un TextView y un Spinner. En el EditText se digita un numero de 6 digitos, al dar click en el Button la aplicacion se comunica con una servidor PHP y una base de datos MYQSL (este proceso lo realizo en la clase public class JSONTask extends AsyncTask<String, Integer, String>), el dato encontrado por ejemplo al digitar 092406 da:

1
[{"NOMBRE":"SILVA TORRES ELIANA","IDENTI":"1151951743","ID_PRO0":"8613","ID_PRO1":"8761","ID_PRO2":"8808","ID_PRO3":"9141","ID_PRO4":"9142","ID_PRO5":"9143"}]


El TextView me muestra NOMBRE y IDENTI, hasta ahi todo me funciona perfectamente, lo que no he podido realizar y es el motivo de mi pregunta, es poder cargar en un SPINNER desde ID_PRO0 hasta IDPRO5 (en este caso 6 ID_PRO), en la web encontre que para retornar multiples valores en doInBackground debo hacer ArrayList as Static y lo hice con:

1
public static ArrayList<String> listFacturas = new ArrayList<>();

He cargado la lista a este ArrayList, pero he tratado de varias maneras y no lo he logrado para que esto se cargue en el SPINNER cuando haya encontrado un NOMBRE y que este NOMBRE tenga ID_PRO, espero su ayuda y poder ver donde fallo. Gracias desde Colombia. Adjunto la clase Java donde se realiza el proceso:

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
package com.windroid.lfp;
 
import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
 
public class Main extends AppCompatActivity {
 
    private TextView miAlumno;
    private Typeface miFont;
    public static String miDato;
    public static ArrayList<String> listFacturas = new ArrayList<>();
    private Spinner miFactu;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Button btAlumno = (Button)findViewById(R.id.btFind);
        miAlumno = (TextView)findViewById(R.id.miTexto);
        miFont = Typeface.createFromAsset(getAssets(),"fonts/courbd.ttf");
        miAlumno.setTypeface(miFont);
        miFactu = (Spinner)findViewById(R.id.cbFact);
 
        final EditText miCodigo = (EditText)findViewById(R.id.edCod);
 
        btAlumno.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (miCodigo.getText().toString().length() < 6) {
                    Context context = getApplicationContext();
                    CharSequence text = "EL CODIGO DEBE DE SER DE SEIS DIGITOS";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                } else {
                    new JSONTask().execute("http://192.168.150.1/lfp/produc.php?ID=" + miCodigo.getText());
 
                }
            }
        });
    }
 
    public class JSONTask extends AsyncTask<String, Integer, String>{
 
        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try{
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
 
                InputStream stream = connection.getInputStream();
 
                reader = new BufferedReader(new InputStreamReader(stream));
 
                StringBuffer buffer = new StringBuffer();
 
                String line = "";
 
                while ((line = reader.readLine()) != null){
                    buffer.append(line);
                }
 
                String finalJson = buffer.toString();
                JSONArray parentArray = new JSONArray(finalJson);
                JSONObject finalObject;
                finalObject = parentArray.getJSONObject(0);
 
                String estNombre = finalObject.getString("NOMBRE");
                if (estNombre == null) {
                    miDato = "ALUMNO NO REGISTRADO";
                } else {
                    NumberFormat formato = new DecimalFormat("#,###");
                    String estIdenti = finalObject.getString("IDENTI");
                    estIdenti = formato.format(Long.valueOf(estIdenti));
 
                    // Verifico que el arreglo contenga ID_PRO
                    int num_fac = finalObject.length() - 2;
                    if (num_fac == 0) {
                        miDato = "ALUMNO:\n" + "  " + estNombre + "\n  " + estIdenti + "\n " + "- SIN MOVIMIENTO";
                    } else {
                        // Cargo los datos ID_PRO en el ArrayList
                        num_fac = finalObject.length() - 3;
                        for (int i = 0; i < num_fac; i++){
                            listFacturas.add(finalObject.getString("ID_PRO"+String.valueOf(i)));
                        }
                    }
                }
                return miDato;
 
            } catch (MalformedURLException e){
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if(connection != null){
                    connection.disconnect();
                }
                try {
                    if (reader != null){
                        reader.close();
                    }
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            return null;
        }
 
 
        @Override
        protected void onProgressUpdate(Integer... progress){
 
        }
 
 
        @Override
        protected void onPostExecute(String result){
            super.onPostExecute(result);
            miAlumno.setText(result); // Aqui muestra perfectamente la informacion en el TextView
        }
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder