Where to do network calls to download JSON for RecyclerView - java

I am writing a small app which queries a REST API to get JSON Data about movies. Which includes
Movie ID
Movie's Poster
URL Movie Overview etc
My main view consists of a RecyclerView which uses a GridLayout Manager to display a grid of movie posters. On tapping, a grid view item would transition to an activity where details of the movie will be presented.
As mentioned above, the API Call would return all important details to build the experience, including the URL to download the Poster.
I will be using Okhttp and Picasso for networking and Image Download. This is my first app which interacts with the network so I need to know where should I put the Okhttp Async Networking Code to get the JSON Data and WHY?
In the Fragment's OnCreate?
In the hosting Activity's OnCreate?
In the RecyclerView's Adapter?
I don't want the code, I just need to know what's the right place to fetch the JSON via Okhttp and why?

You can fetch the JSON via Okhttp in your main fragment or activity, and parse it and store it in an ArrayList, then pass the ArrayList in adapter class, where you need to use picasso library for image download.

Related

get data from api and show in recyclerview

i want to get data from API and show that in RecyclerView, then after click on any items, go to another activity and show the result that a get from another api.
i share image from postman with you and i hope you could help me for this
i'm using android stdio and java and i prefer to use retrofit for call from api
thanks for your attention...
Brands Api for MainActivity.java
Products Api for Second Activity
you need to use Retrofit for getting data from API.. you can checkout a tutorial like this - https://howtodoandroid.com/retrofit-android-example/

Recyclerview in a recyclerview activity

using firebase-ui and some coding I have set up my code to work like this:
1) it retrieves data from firebase (image, text and so on), then it passes them to another activity.
2) the new activity receives datas like strings and shows them.
Just to give you an idea I am looking forward to building a streaming app. What I want to do is to show an episode list via recyclerview.
What I am doing right now is a nested scroll view and a copy-paste script to pass up to 26 episodes, but this is not a really good method and I would like to have some suggestions in order to go forward in the best way and solve this problem.
this is a screenshot of the app: https://imgur.com/a/vgBnZTA I am just passing strings to the 2nd activity, then i check if it is not a null string, if it is i hide the layout, otherwise it shows, but it is just a copypaste of code. I hope i was clear enough
Thanks everyone for your support

Display a gridview with content from an api oncreate

I want to display set the content of gridview to be as gotten from an api.
I used Ion to call in my api and saved them into a generic list of a class i.e(List list;) i used picasso to load images from a url but when set the gridviews adapter in the oncreate it doesn't show the items in the gridview when the app comes up, but when I minimise and bring it back the contents show. I need help making sure it's contents loads up with the app.

Android loading images to ListView AsyncTask

This is more a question of application logic and performance.
Project Setup: I have an Android ListView which inflates a layout that is more aesthetically pleasing than just simple text. In each row (7 to be exact), I have an Image that is fetched by URL.
The problem: This ListView and its data are set by an ArrayList of model objects that serve as temporary data holders. When the rows are added the model objects are then used to set the data in the specific row's UI. The issue then comes along, that when a user scrolls down on the list a row that was once viewable is now out of the user's view. When the user scrolls back up to that row, the whole process of fetching the Image then happens again.
This seems like it can be avoided by instead of passing URLs through the models then fetching the images, I should fetch the images first, then pass the bitmaps to the model, therefore when the row is visible again to the user, it does not have to re-load the image.
Is there a way to write an AsyncTask that can load 7 images successfully, or do I have to create an AsyncTask object for each image?
Everytime I go this route the application crashes...
You can use Picasso library for this task.
Visit http://square.github.io/picasso/
In the adapter of your ListView, in the method getView, you can put this:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_error)
.resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size) .centerInside()
.into(imgView);
You dont need a AsyncTask because this library already implements itself.

Change fragment content when page changed, ViewPager

I have a ViewPager, that i'm populating with fragments which has a listview inside. The point is that i have to show a really big amount of data, and i need to make pages, so i only request littel amounts of data.
My problem is that i need to call an asynctask to retrieve the data when the page is changed, and fill the listview of this page, how can i do this? How can i change that listview in the onPostExecute of the task?
PS: i have used an eclipse template for tabs + swipe activity, so im not posting my code.
I'm not sure I got your problem right, but the first thing that comes to my mind, is that I would use an AsyncTaskLoader instead of a simple AsyncTask. From my (limited) experience, loaders solve a lot of problems when it comes to Fragment/Activity lifecycle/configuration changes.
Loaders guide (Android Developers)
No matter what method you are using to get the data, changing the content of the list view in page B after loading the data in page A shouldn't be much of a problem: you have plenty of options, from simply saving the data for page B in the Activity (then changing the page with setCurrentItem and intercepting the page change event with setOnPageChangeListener),
to a more elegant approach employing a SQLite database (which btw would allow you to do some caching on the results, if possible).
Oh, and let's not forget that, if you are using an implementation of the abstract class FragmentPagerAdapter you can probably pass the data directly from one page to the other, as this PagerAdapter implementation
represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.
(just use getItem(int) to get a reference to the page you need. I never tried this myself, though).
Hope this helps
EDIT I just found out that getting a reference to the current Fragment shown in the ViewPager is tricky if you are not using a FragmentPagerAdapter: if this was the root of your problem, information about how it can be done can be found here:
Update data in ListFragment as part of ViewPager
Retrieve a Fragment from a ViewPager
Communication between ViewPager and current Fragment

Categories