How to populate listview with volley consultation

0

You can help me to populate a ListView of a query with volley + php + mysql in android studio .

I leave the code for these classes, as you can see the content in a textView but I want to show it in a ListView and I do not know how to do it.

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 07.06.2017 в 08:50
source

2 answers

0

First you must create the ListView and its adapter to fill it.

link

    
answered by 07.06.2017 в 13:10
0

IF what you want is to know how to create the adapter for your LISTVIEW I leave you a small example

First declare your ListView

 final ListView lstAuxBilling =findViewById(R.id.lstbillingnotifications);

After this you must create an Adapter which you will set to your ListView, For now stay with this part

     AdapterCobroPendiente adapter = new AdapterCobroPendiente(BillingNotifications.this, items);
lstAuxBilling.setAdapter(adapter);

The Adapter Class:

 public class AdapterCobroPendiente extends BaseAdapter {
    protected Activity activity;
    protected List<CobroPendiente> items;
    Context context ;
    public AdapterCobroPendiente(Activity activity, List<CobroPendiente> itemsCompra) {
        this.activity = activity;
        this.items = itemsCompra;
        context = activity;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Integer.parseInt(items.get(position).getNumber());
    }

    @Override
    public View getView(int position, final View contentView, ViewGroup parent) {
        View vi = contentView;
        final int pos = position;
        if (contentView == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                vi=inflater.inflate(R.layout.activity_billing_notifications_adapter, null);
        }
        DecimalFormat formater = new DecimalFormat("###.00");
        CobroPendiente item = items.get(position);
        TextView mount =  vi.findViewById(R.id.txtMountBillingNotifications);
        mount.setText(activity.getString(R.string.MonedaMonto) + " "+formater.format(Double.parseDouble(item.getMonto())));

        TextView date = vi.findViewById(R.id.txtDateBillingNotifications);
        date.setText(item.getFecha());



        TextView name = vi.findViewById(R.id.txtNumberPhoneBilling);
        name.setText(item.getNombreOrigen());



        TextView referencia = vi.findViewById(R.id.txtDateBillingRef);
        referencia.setText(item.getReferencia());
        ImageView btnAcceptBillingNotifications =  vi.findViewById(R.id.btnAcceptBillingNotifications);
        btnAcceptBillingNotifications.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final ProgressDialog progressDialog;
                progressDialog = new ProgressDialog(activity);
                progressDialog.show();
                progressDialog.setContentView(R.layout.progressbar);
                progressDialog.setCancelable(false);
                progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));




        ImageView btnCancelBillingNotifications =  vi.findViewById(R.id.btnCancelBillingNotifications);
        btnCancelBillingNotifications.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               ///
                final ProgressDialog progressDialog;
                progressDialog = new ProgressDialog(activity);
                progressDialog.show();
                progressDialog.setContentView(R.layout.progressbar);
                progressDialog.setCancelable(false);
                progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

                //////////


}
        return vi;
    }
}

in my case in adapter view is the following

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="@android:color/white">

<TextView
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:id="@+id/txtNumberPhoneBilling"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:text="1"
    android:ellipsize="end"
    android:maxLines="1"
    android:textSize="16dp"
    android:textColor="@color/colorFondoHomeAzulado"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:textColor="@color/HintHomeMonto"
    android:id="@+id/txtDateBillingNotifications"
    android:layout_width="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/txtNumberPhoneBilling"
    android:text="3"
    android:textSize="12dp" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtNumberPhoneBilling">
    <TextView
        android:id="@+id/txtDateBillingRef"
        android:layout_width="160dp"
        android:layout_marginLeft="20dp"
        android:ellipsize="end"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="2"
        android:textColor="@color/HintHomeMonto"
        android:layout_gravity="center_vertical"
        android:textSize="14dp"
        android:maxLines="1" />

    <TextView
        android:layout_centerVertical="true"
        android:id="@+id/txtMountBillingNotifications"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorFondoHomeAzulado"

        android:layout_toRightOf="@+id/txtDateBillingRef"
        android:text="4"
        android:textSize="20dp" />
</RelativeLayout>


<ImageView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnCancelBillingNotifications"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/btnAcceptBillingNotifications"
    android:background="@mipmap/rechazarxxxhdpi"
    android:layout_centerInParent="true" />
<ImageView
    android:id="@+id/btnAcceptBillingNotifications"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="30dp"
    android:background="@mipmap/aceptarhdpi"

    android:layout_centerInParent="true" />

Remember that items that are passed to the adapter, is a list < > in this case from my own class, but you could pass a list < > of Strings

    
answered by 21.05.2018 в 19:44