Why does the Recycler View sometimes load the Json obtained and sometimes not?

1

First of all, thank you very much for reading my question. I am new to this and I hope to formulate the query correctly. I have an app which I consult through volley a URL that a Json returns to me. Then this Json is taken to each of the items that are in a Cardview, which is in a RecyclerView.

The issue is that this works sometimes !, that is, once I load the Recycler with the articles and again not ... I test with a TOAST the times that the Recycler does not load me and I see that if it brings me the JSON but it does not reflect it ... try an asyntask and nothing ...

my app has login for this reason to test if it loads the Recycler I have to log out and then log in again to see if I'm lucky and I load the recycleview.

My class that generates the recycler's filling

public class ScrollingActivity extends AppCompatActivity {

List<ArticulosBean> list = new ArrayList<>();
private RecyclerView recyclerView;
private ArticulosAdapter pAdapter;
private ProgressDialog progressDialog;
private static final String URL = "https://www.servicioswebtsas.com/WebServicePedidos/lista_articulos.php";


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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    pAdapter = new ArticulosAdapter(list, getApplicationContext());

    // Create grids with 2 items in a row
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(pAdapter);


}

@Override
public void onResume() {
    super.onResume();
}


@Override
protected void onStart() {
    super.onStart();

    final StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray jsonArray = new JSONArray(response);

                    Log.d("HOLAA", response.toString());
                    //ATENCION: ACÁ TENGO QUE PONER UN IF POR SI NO HAY ARTICULOS EN BASE DE DATOS. LA BASE NUNCA VA A ESTAR VACIA.
                    for (int i = 0; i < jsonArray.length(); i++) {
                        ArticulosBean articulos = new ArticulosBean(jsonArray.getJSONObject(i).getString("nombre_articulo"),
                                jsonArray.getJSONObject(i).getString("imagen_url"), //PUEDO PONER UN STRING GRACIAS A LA LIBRERIA PICASSO EN ADAPTER
                                jsonArray.getJSONObject(i).getDouble("precio"),
                                jsonArray.getJSONObject(i).getInt("stock"));
                        list.add(articulos);
                    }
                Toast.makeText(getApplicationContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                Log.d("PENDRIVE", e.toString());
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Toast.makeText(getApplicationContext(), "ERROR: " + error, Toast.LENGTH_SHORT).show();
            Log.d("ERROR LISTA", error.toString());
        }
    });

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

}


//METODO QUE ESCUCHA BOTTON REGRESAR Y PREGUNTA SI REALMENTE QUIERE SALIR.
@Override
public void onBackPressed() {

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Add the buttons
    builder.setMessage(R.string.msg_salir);
    builder.setTitle(R.string.msg_tit_salir);

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            System.exit(0);
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog

        }
    });
    // Set other dialog properties
    // Create the AlertDialog
    builder.show();
    builder.setCancelable(false);
    AlertDialog dialog = builder.create();

}
    
asked by Lucas Jose Sola 06.11.2018 в 15:46
source

1 answer

0

I think what you need is the following:

When you get the data of the call, you put them in the list that (I suppose) is the model to show (you passed it to the Adapter in the constructor). What you do not do is tell the adapter that the list has changed, which you can do by adding just after the loop where you put the elements.

pAdapter.notifyDataSetChanged();

I think when it works for you it's why it's called an race condition : You have the answer before generating the view for the first time, but when you use the mobile data there is a slight delay in the response which causes that the view has already been generated, which is then never updated.

    
answered by 06.11.2018 / 16:44
source