如何在Spinner中加载ArrayList

时间:2015-10-06 18:59:59

标签: java android

我有MySql数据库,这是我的JSON文件:

[{"NAME":"SILVA TORRES ELIANA","ID":"1151951743","INVOICE0":"8613","INVOICE1":"8761","INVOICE2":"8808","INVOICE3":"9141","INVOICE4":"9142","INVOICE5":"9143"}]

我的Android应用程序有EditText,Button,TextView和Spinner,当用户点击按钮时,应用程序显示NAME和ID,但我无法在Spinner中加载INVOICE。

这是Java类:

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("NAME");
                if (estNombre == null) {
                    miDato = "ALUMNO NO REGISTRADO";
                } else {
                    NumberFormat formato = new DecimalFormat("#,###");
                    String estIdenti = finalObject.getString("ID");
                    estIdenti = formato.format(Long.valueOf(estIdenti));

                    // ID_PRO > 0
                    int num_fac = finalObject.length() - 2;
                    if (num_fac == 0) {
                        miDato = "ALUMNO:\n" + "  " + estNombre + "\n  " + estIdenti + "\n " + "- SIN MOVIMIENTO";
                    } else {
                        // Load ID_PRO in 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); // Displays NAME and ID in TextView
        }
    }
}

感谢。

1 个答案:

答案 0 :(得分:2)

使用ArrayAdapter。

http://developer.android.com/reference/android/widget/ArrayAdapter.html

ArrayAdapter<String> my_adapter = new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item, listFacturas);