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";
}