0

I'm new to Android development so please bear with me.

So I am trying to use HttpURLRequest to retrieve a list of items and populate those into a RecyclerView. I am running the web request in a separate thread, but I am having a hard time figuring out how to get the results into the UI. I've tried updating the adapter from the other thread but it tells me I can't update from that thread.

So question is, what is best practice for fetching a list from the web and displaying the results in a RecyclerView.

  • since you are getting data from a thread and trying to populate data in UI Thread. have you tried the function "runOnUIThread" ?? – Muhammad Faizan May 05 '20 at 02:45
  • Simple practice to show data in recycler view from web http is populating an ArrayList and after you have fetched the data passing the list to the recycler view adapter and inflating each item and make sure that you put the size of recyclerview list item to your own populated list – Muhammad Faizan May 05 '20 at 02:48
  • When I try that I get an android.os.NetworkOnMainThreadException error. – Chris Gulka May 05 '20 at 12:22
  • When I try using an AsyncTask I can't figure our how to update the UI. – Chris Gulka May 05 '20 at 12:23
  • I was able to get this working by properly implementing a handler – Chris Gulka May 06 '20 at 03:09
  • sry for late reply but if your app has orientation enabled than AsyncTask will be a big problem. – Muhammad Faizan May 06 '20 at 13:02
  • Hmm, how so? Can you explain a bit? – Chris Gulka May 07 '20 at 02:40
  • Screen orientation change in android causes your activity to restart again means it will be destroyed and recreate. If you execute your network call and mean while change your screen orientation your app will crash. reason is that AsyncTask is hooked to the instance of your old activity, not your new activity and you will get NullPointerException. For more details read this. https://stackoverflow.com/a/26092743/7798783 – Muhammad Faizan May 07 '20 at 15:00

0 Answers0