Cardview does not display JSON information

Asked

Viewed 312 times

15

Guys I’m trying to set some information in a Cardview, where these are obtained from a JSON, however, it does not display, the information search this ok, it returns the values normally but does not display in the app..

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        mList = savedInstanceState.getParcelableArrayList("mList");
    } else {
        mList = getCarsByCategory(0);
    }
}

method getSetCarList()

public List<Car> getSetCarList(final int qtd, final int category) {

    final String[] models = new String[qtd];
    final String[] brands = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",};
    final int[] categories = new int[]{2, 1, 2, 1, 1, 4, 3, 2, 4, 1};
    final int[] photos = new int[]{R.drawable.gallardo, R.drawable.vyron, R.drawable.corvette, R.drawable.paganni_zonda, R.drawable.porsche_911, R.drawable.bmw_720, R.drawable.db77, R.drawable.mustang, R.drawable.camaro, R.drawable.ct6};
    final String description = "Lorem Ipsum é simplesmente uma simulação de texto da indústria tipográfica e de impressos, e vem sendo utilizado desde o século XVI, quando um impressor desconhecido pegou uma bandeja de tipos e os embaralhou para fazer um livro de modelos de tipos. Lorem Ipsum sobreviveu não só a cinco séculos, como também ao salto para a editoração eletrônica, permanecendo essencialmente inalterado. Se popularizou na década de 60, quando a Letraset lançou decalques contendo passagens de Lorem Ipsum, e mais recentemente quando passou a ser integrado a softwares de editoração eletrônica como Aldus PageMaker.";
    mList = new ArrayList<>();
    //carAdapter = new CarAdapter(this, listCars);
    //carAdapter.onAttachedToRecyclerView(new CarFragment().recycler());

    RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());

    Log.e("START GET", "STARTED GET");
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            URL_FEED, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            try {

                JSONArray feedArray = jsonObject.getJSONArray("data");

                for (int i = 0; i < models.length; i++) {
                    JSONObject feedObj = (JSONObject) feedArray.get(i);
                    models[i] = feedObj.getString("message");
                    Log.i("MESSAGE", "------ POSITION: " + i + ", MESSAGE:" + models[i]);
                }

                for (int i = 0; i < qtd; i++) {
                    Car c = new Car(models[i % models.length], brands[i % brands.length], photos[i % models.length]);
                    Log.e("MODELS", "MESSAGE: " + models[i]);
                    c.setDescription(description);
                    c.setCategory(categories[i % brands.length]);
                    c.setTel("33221155");

                    if (category != 0 && c.getCategory() != category) {
                        continue;
                    }

                    mList.add(c);
                }

                Log.e("FINISH INSERT", "FINISH INSERT");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.e("ERRO JSONREQUEST", "ERRO: " + volleyError.toString());
        }
    });

    mRequestQueue.add(jsonObjReq);
    Log.i("FINISH REQUEST", "FINISH REQUEST");
    /*for (int i = 0; i < qtd; i++) {
        Car c = new Car(models[i % models.length], brands[i % brands.length], photos[i % models.length]);
        Log.e("MODELS", "MESSAGE: " + models[i]);
        c.setDescription(description);
        c.setCategory(categories[i % brands.length]);
        c.setTel("33221155");

        if (category != 0 && c.getCategory() != category) {
            continue;
        }

        listAux.add(c);
    }*/

    return (mList);
}

getCarsByCategory method()

 public List<Car> getCarsByCategory(int category) {
    List<Car> listAux = new ArrayList<>();
    for (int i = 0; i < mList.size(); i++) {
        if (category != 0 && mList.get(i).getCategory() != category) {
            continue;
        }

        listAux.add(mList.get(i));
    }
    return (listAux);
}

https://www.filepicker.io/api/file/Nz4jE7XBR8aEukAtfkko

  • If you say the information is returned correctly then instead of posting the code that works you should post the one where you think the problem is.

  • That’s the problem I don’t know where this problem rsrs.... when I passed information already from the application itself it displayed normal, after I passed to the JSON information it does not display

  • Post the part of the code that calls this method.

  • I found a possible error, when I declare the variables as final, it does not display, if I take the end it from the error and asks for final. But when you take out the part that inserts the Jsonrequest information and put any information, without the end it displays.

  • @Carlos posts the layout code. And the code where you bind the information that is coming from your request(Json) with the layout. Because apparently this code is missing. For example, I have not seen the setContentView of his Activity.

  • 1

    You see, this request is asynchronous, I haven’t seen the code where you populate the cardview itself. You only posted the codes where you search the JSON values. I suspect that in fact the carde is empty because at the moment of creation the JSON data have not yet arrived precisely because it is asynchronous. Either you populate cardview only after JSON returns or you implement a method that updates cardview after JSON returns.

  • Probably the error is in the Json parse. STOP suffering, use Retrofit http://square.github.io/retrofit/ plus your code will be much cleaner...

  • I agree with @Waltergandarella. Your problem seems to be in the wrong assumption that your method is apt to return a List<Car>, when it is not. Manually add an item to this mList after creating it in getSetCarList to see if it appears in the results list.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.