0

I am building an application that takes json array data and add it to a recyclerview. I was able to parse the data successfully in the below code.

 private void parseJsonDetails(String jsonDetails) throws JSONException {

    JSONObject parentObject = new JSONObject(jsonDetails);
    JSONArray parentArray = parentObject.getJSONArray("stores");


    final DataModel[] models = new DataModel[parentArray.length()];

    for (int i = 0; i < parentArray.length(); i++) {

        JSONObject storeObject = parentArray.getJSONObject(i);
        DataModel dataModel = new DataModel();
        dataModel.setCity(storeObject.getString("city"));
        dataModel.setName(storeObject.getString("name"));
        dataModel.setLatitiude(storeObject.getDouble("latitude"));
        dataModel.setLongtitude(storeObject.getDouble("longitude"));
        dataModel.setZipcode(storeObject.getInt("zipcode"));
        dataModel.setStoreLogoUrl(storeObject.getString("storeLogoURL"));
        dataModel.setPhone(storeObject.getString("phone"));
        dataModel.setStoreId(storeObject.getString("storeID"));
        dataModel.setState(storeObject.getString("state"));
        dataModel.setAddress(storeObject.getString("address"));

        models[i] = dataModel;
    }

After parsing I tried adding it to the recyclerview (as seen below). By passing in the models[] array and the context into the recyclerview Adapter. But i got the error - "Only the original thread that created a view hierarchy can touch its views"

           models[i] = dataModel;
            }

            storeAdapter = new StoreAdapter(models, this);
            recyclerView.setAdapter(storeAdapter);
            recyclerView.setHasFixedSize(true);

            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setHasFixedSize(true);
        }
    }

Now I did some research about this error on this site and it states that background tasks that updates the views should be run on the UI thread. But how is this done with a recyclerview? How do you add the parsed data into the recyclerview adapter without getting this error?

The parseJsonDetails is being called from the onResponse method after an API call.

  @Override
        public void onResponse(Response response) throws IOException {

            //Add catch around these methods
            String jsonDetails = response.body().string();

            if (response.isSuccessful()) {

                try {
                    parseJsonDetails(jsonDetails);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
mustyg123
  • 17
  • 5
  • 3
    Possible duplicate of [Android "Only the original thread that created a view hierarchy can touch its views."](https://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) – Nabin Bhandari Jun 09 '18 at 16:51
  • What's calling your ParseJSON mehod? If using the typical Async task. You change your ParseJSON to return data model. Then in your async task on postexecute method you do the rest of your code. – Joel Libby Jun 09 '18 at 16:52
  • @JoelLibby, it's being called in the onresponse method after the call has been executed. – mustyg123 Jun 09 '18 at 20:02

0 Answers0