A query. In my code I am creating a list of CardViews from a JSONArray that I record with a for and I am filling a list to create the CardViews, only that sometimes if I create them and sometimes I do not know why, I leave my code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promos_por_concepto);
Intent promosIntent = getIntent();
final Bundle promosExtras = promosIntent.getExtras();
cp_id = promosExtras.getString("cp_id");
listaPromosConcepto = new ArrayList<>();
//Llamamos método para crear cards
CreaCard();
}
public void CreaCard(){
//Recibimos la respuesta del Json filtrada por el cp_id
Response.Listener<String> promosConceptoListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray promosArray = new JSONArray(response);
for (int y = 0; y < promosArray.length(); y++) {
JSONObject promosObjectArray = promosArray.getJSONObject(y);
JSONArray promoConceptoArray = promosObjectArray.getJSONArray("promos");
for (int i = 0; i < promoConceptoArray.length(); i++) {
JSONObject promoConcepto = promoConceptoArray.getJSONObject(i);
String promoDesc = promoConcepto.getString("prom_desc");
String neg_Nomb = promoConcepto.getString("neg_nombre");
String calificacion = promoConcepto.getString("calificacion");
String promImgUrl = promoConcepto.getString("logo");
listaPromosConcepto.add(new FuentePromosPorConcepto("Título promo", neg_Nomb, promoDesc, promImgUrl, calificacion));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
PromosInfoConceptoRequest promosInfoConceptoRequest = new PromosInfoConceptoRequest(cp_id, promosConceptoListener);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(promosInfoConceptoRequest);
final RecyclerView recyclerPorConcepto = findViewById(R.id.recyclerContenedorDetalle);
recyclerPorConcepto.setHasFixedSize(true);
LinearLayoutManager layout = new LinearLayoutManager(getApplicationContext());
layout.setOrientation(LinearLayoutManager.VERTICAL);
recyclerPorConcepto.setAdapter(new PromosPorConceptoAdapter(listaPromosConcepto));
recyclerPorConcepto.setLayoutManager(layout);
PromosPorConceptoAdapter adapter = new PromosPorConceptoAdapter(listaPromosConcepto);
recyclerPorConcepto.setAdapter(adapter);
}