People ListView with Volley + php + mysql

0

I need to populate the listview , I am not able to display the data in ListView . I pass the code to you, in this case I show the data in TextView .

public class BuscarPartidos extends AppCompatActivity implements View.OnClickListener {


    private EditText edConsultaDeporte;
    private Button btnConsultaDeporte;
    private TextView tvCuadro;
    private ProgressDialog cargando;
    private Button ConsultaTodos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buscar_partidos);

        edConsultaDeporte = (EditText) findViewById(R.id.edConsultaDeporte);
        btnConsultaDeporte = (Button) findViewById(R.id.btnConsultaDeporte);
        tvCuadro = (TextView) findViewById(R.id.tvCuadro);
        btnConsultaDeporte.setOnClickListener(this);

    }

    private void consultaDeporte() {
        String deporte = edConsultaDeporte.getText().toString().trim();
        if (deporte.equals("")) {
            Toast.makeText(this, "Por favor, introduce algún deporte", Toast.LENGTH_LONG).show();
            return;
        }
        cargando = ProgressDialog.show(this,"Cargando","Actualizando",false,false);
        String url = Consultas.DATA_URL_DEPORTE+edConsultaDeporte.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                cargando.dismiss();
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(BuscarPartidos.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }



    private void showJSON(String response){
        String deporte="";
        String nivel="";
        String numjug = "";
        String polideportivo = "";
        String fechapar = "";
        String horapar = "";

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray result = jsonObject.getJSONArray(Consultas.JSON_ARRAY);
            JSONObject collegeData = result.getJSONObject(0);
            deporte = collegeData.getString(Consultas.KEY_DEPORTE);
            nivel = collegeData.getString(Consultas.KEY_NIVEL);
            numjug = collegeData.getString(Consultas.KEY_NUMJUG);
            polideportivo = collegeData.getString(Consultas.KEY_POLIDEPORTIVO);
            fechapar = collegeData.getString(Consultas.KEY_FECHAPAR);
            horapar = collegeData.getString(Consultas.KEY_HORAPAR);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        tvCuadro.setText("Fecha: " + fechapar + "Hora: " + horapar);
    }


    @Override
    public void onClick(View v) {
        consultaDeporte();
    }
}

This is the other class I use.

public class Consultas {

        //URL del fichero PHP de consulta para deporte
        public static final String DATA_URL_DEPORTE = "http://proyecto2gs.esy.es/consultaDeporte.php?deporte=";
        public static final String DATA_URL_consultaTodos = "http://proyecto2gs.esy.es/consultaTodos.php";
        //Tabla Partidos
        public static final String KEY_DEPORTE = "deporte";
        public static final String KEY_NIVEL = "nivel";
        public static final String KEY_NUMJUG = "numjug";
        public static final String KEY_POLIDEPORTIVO = "polideportivo";
        public static final String KEY_FECHAPAR = "fechapar";
        public static final String KEY_HORAPAR = "horapar";
        public static final String JSON_ARRAY = "result";
}
    
asked by javier guerrero 05.06.2017 в 19:05
source

1 answer

0

I think that you are assigning values to the query that you bring, you must first modify your class Queries should be as follows:

    public class Consultas {

    //URL del fichero PHP de consulta para deporte
    public static final String DATA_URL_DEPORTE = "http://proyecto2gs.esy.es/consultaDeporte.php?deporte=";
    public static final String DATA_URL_consultaTodos = "http://proyecto2gs.esy.es/consultaTodos.php";
    //Tabla Partidos
    public static final String KEY_DEPORTE = "";
    public static final String KEY_NIVEL = "";
    public static final String KEY_NUMJUG = "";
    public static final String KEY_POLIDEPORTIVO = "";
    public static final String KEY_FECHAPAR = "";
    public static final String KEY_HORAPAR = "";
    public static final String JSON_ARRAY = "result"; } 

Then, in your query Json:

    try {
        JSONObject jsonObject = new JSONObject(response);
        KEY_DEPORTE = collegeData.getString(Consultas.KEY_DEPORTE);
        KEY_NIVEL = collegeData.getString(Consultas.KEY_NIVEL);
        KEY_NUMJUG = collegeData.getString(Consultas.KEY_NUMJUG);
        KEY_POLIDEPORTIVO = collegeData.getString(Consultas.KEY_POLIDEPORTIVO);
        KEY_FECHAPART = collegeData.getString(Consultas.KEY_FECHAPAR);
        KEY_HORAPART = collegeData.getString(Consultas.KEY_HORAPAR);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    tvCuadro.setText("Fecha: " + KEY_FECHAPART + "Hora: " + KEY_HORAPART);
}

IN your code you use the
JSONArray result = jsonObject.getJSONArray (Queries.JSON_ARRAY); which is to treat json fixes, whereas as I see you want to show is an object.

Anyway, if you have any doubts, please let me know in this way and clarify them as soon as possible

    
answered by 06.09.2017 в 22:35