How to reload RecyclerView

0

Hello my people I have a doubt that is breaking my head and I have already taken much of the day. I have a RecyclerView in android which I want to recharge once I do an action on the adapter and I do not know how to do it. Here I show you part of my code. This is where I make the call to the Delete option which removes a product through an option in the recycler view.

 holder.menuDesbordamientoCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu popup = new PopupMenu(context, holder.menuDesbordamientoCardView);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.desbordamiento_cardview_producto,popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    //do your things in each of the following cases
                    switch (item.getItemId()) {
                        case R.id.eliminarProductoId:

                            Productos productos=new Productos();
                            Toast.makeText(context, holder.id.getText(), Toast.LENGTH_SHORT).show();
                            productos.deleteProducto(holder.id.getText().toString(),context);

                           context.startActivity(new Intent(context,Productos.class));

                            return true;

                        default:
                            return false;
                    }
                }
            });
            popup.show();
        }
    });

The next thing I show you is my delete method in which I do the delete action and I do not know how to get my RecyclerView loaded again.

public void getProductos(final Context context) { // Funcionando correctamente



    final StringRequest stringRequest = new StringRequest(Request.Method.GET, IP_ROUTE + "facturasapi/public/productos", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            // Toast.makeText(Productos.this, response, Toast.LENGTH_SHORT).show();
            JSONArray obj = null;
            ProductosModel productosModel = new ProductosModel(null, null, null);
            ArrayList<ProductosModel> arrayList = new ArrayList<>();
            try {
                obj = new JSONArray(response);

                JSONObject jsonArray;

                for (int i = 0; i <= obj.length(); i++) {
                    jsonArray = obj.getJSONObject(i);
                    int id = jsonArray.getInt("id");
                    String codigo = jsonArray.getString("codigo");
                    String descripcion = jsonArray.getString("descripcion");
                    String precio = jsonArray.getString("precio");
                    arrayList.add(new ProductosModel(id, codigo, descripcion, Double.valueOf(precio)));
                }


            } catch (JSONException e) {
                e.printStackTrace();
            }




        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, error + "", Toast.LENGTH_SHORT).show();
        }
    }
    ) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };


    Log.d("Resultado ", stringRequest.toString());
    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);
}

The application always gives me context errors.

Initialization of the Recycler View is done in the same java class as the previous method. I show you my Oncreate. It is worth noting that the method works well when the activity starts but not when I try to call it when trying to delete a product to show the new list.

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

    context = getApplicationContext();

    //Quitamos barra de notificaciones
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mRecyclerView = findViewById(R.id.my_recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    getProductos(context);


    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    fab = findViewById(R.id.fabProductos);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Productos.this, NuevoProducto.class);
            startActivity(intent);
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    ocultarFAB(mRecyclerView);

}
    
asked by Yasnier Perdigon Lorenzo 01.04.2018 в 22:01
source

1 answer

0

notifyDataSetChanged () performs a reload of the adapter. But you have to update your list of Products objects. For example if you delete a product, remove it from the list you pass to the adapter and then call the notifyDataSetChanged () method.

    
answered by 02.04.2018 в 10:49