How do I call the method requestCoffee to the method on click

1

In the onClick method I have to add the call to the requestCoffe method but I do not know how to do it

private void requestCoffe(){
    RequestQueue queue= Volley.newRequestQueue(this);
    JsonArrayRequest arrayRequest=new JsonArrayRequest(Request.Method.GET,
            URL, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            coffees=new ArrayList<>();
            try {
                for (int i=0; i < response.length(); i++){
                    Coffee coffee=new Coffee(response.getJSONObject(i));
                    coffees.add(coffee);
                }
                adapter=new CoffeeAdapter(coffees, MainActivity.this); //Modificar la creación del Adapter, agregando
                // la referencia a la clase como segundo argumento.
                recyclerCoffee.setAdapter(adapter);
            } catch (JSONException e){
                e.printStackTrace();
            }
        }
        }, new Response.ErrorListener(){
            @Override
                    public void onErrorResponse(VolleyError error){
                Toast.makeText(MainActivity.this, "No se puede conectar",
                        Toast.LENGTH_SHORT).show();

        }
    });
    queue.add(arrayRequest);
}

public void onClick(View view) {

}
    
asked by Gutty Zamorano Capdevila 29.06.2018 в 22:14
source

1 answer

-1

Your class should be implementing View.OnClickListener

 implements View.OnClickListener {

the onClick () method must be overwritten:

   @Override
    public void onClick(View v) {

    }

and designate the listener at sight that would call it, for example:

myButton.setOnClickListener(this);

To make the Volley request, add the call of the requestCoffe() method within onClick() :

   @Override
    public void onClick(View v) {

      requestCoffe();

    }

Although I recommend that the definition of RequestQueue you can do before, for example within onCreate() of your Activity

   private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        queue = Volley.newRequestQueue(this);
        ...
      }

since you do not need to define it every time you make the request

private void requestCoffe(){
    //* RequestQueue queue= Volley.newRequestQueue(this);
    JsonArrayRequest arrayRequest=new JsonArrayRequest(Request.Method.GET,
            URL, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            coffees=new ArrayList<>();
            try {
                for (int i=0; i < response.length(); i++){
                    Coffee coffee=new Coffee(response.getJSONObject(i));
                    coffees.add(coffee);
                }
                adapter=new CoffeeAdapter(coffees, MainActivity.this); //Modificar la creación del Adapter, agregando
                // la referencia a la clase como segundo argumento.
                recyclerCoffee.setAdapter(adapter);
            } catch (JSONException e){
                e.printStackTrace();
            }
        }
        }, new Response.ErrorListener(){
            @Override
                    public void onErrorResponse(VolleyError error){
                Toast.makeText(MainActivity.this, "No se puede conectar",
                        Toast.LENGTH_SHORT).show();

        }
    });
    queue.add(arrayRequest);
}
    
answered by 30.06.2018 в 01:59