Sometimes creates the cardview and sometimes does not

0

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);
}
    
asked by Andressualu 05.04.2018 в 23:09
source

1 answer

1

What I think happens is that you are making a request with volleyball. As your request is asynchronous, you do not know at what moment you have information in your listaPromosConcepto arrangement. Some times when you initialize your adapter, you may or may not have received the information you requested with volley (that's why sometimes it shows something and sometimes it does not)

What you have to do is after receiving the data of your request make a notifyDataSetChanged on your adapter, to refresh the content of your list.

private PromosPorConceptoAdapter adapter;


public void CreaCard(){
    adapter = new PromosPorConceptoAdapter(listaPromosConcepto);
    Response.Listener<String> promosConceptoListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //etc etc rellenar arreglo
                //hacer esto fuera de los 2 ciclos for
                adapter.notifyDataSetChanged();
            }
    };

Note: you are assigning 2 times to your recyclerByConcept the adapter, do it only once.

    
answered by 06.04.2018 в 00:36