Ante todo muchas gracias por leer mi pregunta. Soy nuevo en esto y espero formular la consulta correctamente. Tengo una app la cual consulto a través de volley una URL que me regresa un Json. Luego este Json es llevado a cada uno de los items que, están en un Cardview, que este se encuentra en un RecyclerView.
El tema es que esto funciona a veces!, es decir una vez me carga el Recycler con los articulos y otra vez no... testeo con una TOAST la veces que no me carga el Recycler y veo que si me trae el JSON pero no lo refleja... intente con un asyntask y nada...
mi app tiene login por tal motivo para probar si carga el Recycler tengo que desloguearme y luego loguearme otra vez para ver si tengo suerte y me carga el recycleview.
Mi clase que genera el llenado del recycler
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();
}