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();
}