I am trying to understand that what is loaders. Can anyone share an example with it? I don't know when we can use multiple loaders in an activity or fragment. I can't figure out one instance of multiple loaders to implement.
Loaders, while commonly used to populate lists, can be used for a whole host of things. Basically, anything you to do on a separate thread can be done in a loader. If you need to make multiple calls to the network and need to do different things when you get the results, that's when you'd use multiple loaders. You could also use one loader to populate a list with a cursor, and another loader to do network calls.
I don't know when we can use multiple loaders in an activity or
fragment. I can't figure out one instance of multiple loaders to
implement.
Here you go!
Let's suppose you are making a news app.
You have a ListView/RecyclerView on your Launcher Activity which displays the news. Each of your listItem has one ImageView to display thumbnail, two TextViews - one for news article title & another for news article category(Ex: Politics, Sports, Technology).
Now, in order to get news, you have to fetch data from a remote server(website) using their API. And when you fetch data, that website returns data in the form of JSON.
You have to connect to that website, get the JSON, parse that JSON (i.e. extract news article title, news article category, thumbnail_URL). Then you have to download thumbnails from extracted thumbnail_URL and bind data to your ListView/RecyclerView.
In this case, you could use one Loader for parsing JSON; use another Loader to download thumbnails from extracted thumbnail_URL.
Related
Hello Dear Developers,
I am working on a project which uses Flickr API, I parsed the data and took the necessary values from JSONArray and JSONObject, however the URL does not provide all the necessary information about the user (the user who shared the photo on Flickr ) for this reason I decided to use flickr.people.getInfo but in this situation, how can I handle response JSON, I mean if I parse this URL, I have to change RecyclerView Adapter, ViewHolder, but it is not sufficient way of handling multiple different API calls.
I hope, the explanation is clear, if it is not pleasing make comment then I will provide answers to your specific questions.
If I understood correctly you should make two calls, then merge responses together and show result into UI.
You can handle this with callbacks or RxJava observables.
The main idea is to create a single class - model that will contain all data you need to present on your UI. Then make the first network call and parse the response into this object. Pass this object to the next network call and update it with the corresponding user data. And only after these two calls are done and you collected all needed data you can insert the model into RecyclerView.
Hope it helps.
Question: Assuming I have a web service which returns a JSON which I deserialize into a (list of) POJO(s), should each Fragment get a copy of the datasource or should I keep the datasource only in the activity and let the fragment(s) get the data via a callback interface (which I set in onAttach())? Or is there even another, better way?
WHY
I work with Android for quiet a while now but I think I still have not fully understood some very basic principles and I hope you can help me clarify things. In one of my applications, I use Volley to call a web service which returns JSON formatted data.
I use GSON to deserialize the JSON into POJOs. I then use greenrobots EventBus to send the new data around so I can update the UI. Currently all my fragments are registered as subscribers and each fragment stores a reference to the data.
I think I am doing this wrong because what if a fragment is currently not being displayed (they unregister from EventBus in onStop()). They could miss an update of the model and when a Backstack is popped, they could display an outdated model, right?
So what would be the best way to store the model so that all my UI components and controllers always display the latest version of the model? I'm fearing that one or the other component (Fragment and/or Activity) might miss an update and then displays outdated data.
What is Androids way to store models retrieved from web services and make them accessible for activities and fragments?
I think my main problem is that I can refresh the data in multiple activities and fragments. For example I could reload the data after a pull-to-refresh of a list of entries but renaming an entry would also cause an update - but only of this particular entry.
Have you thought about using sqlite to store the data and then displaying it?
What I would do is as follows:
Fetch data from service as a json
Parse the json and store it in a sqlite table
Read from the sqlite table as and when required
The advantage in doing so is that you are able to store new data as well as update the old one and as you said you can use pull down to refresh functionality, this proves helpful to store the latest data as well as the old one and always display the latest data as you are fetching from a datasource that is being updated.
I have to query on 6/7 remote datasets through APIs and then show the results in fragments, I am wondering which solution would be the most performant and better for ux.
Should I create a CursorLoader for each fragment or implement another service which use one response at time to create the fragments and show them?
I'm working on a movie app, when the user first open the app I would fetch the most current popular movie data and populate it in my ListView. The initial fetch provide a list of movies, but only provide the basic information to fill a ListView (id, name, rating, release data, poster image url). To get the remaining data (run time, tagline, description, movie website, etc), I would have to fetch from /movies/{id} endpoint for each of the movies. Also, for a list of trailer, I would need to fetch from /movies/{id}/videos. Currently I'm fetching the movie detail data when they select the movie on the list using AsyncTask and then setting the Views with the data on the initial detail loading. I'm caching the data in a Content Provider and SQLite.
My questions are:
How do I prefetch the data after getting the initial list of movie
from the first fetch? I'm currently using an AsyncTask for the
initial fetch, do I just start a list of AsyncTask in the middle of
the initial task?
Should I even be using AsyncTask for this situation or are there
better alternatives? It seems like a SyncAdapter is a good idea for
the initial fetch, but not sure what is best to use for the
additional detail/trailer.
I just started working with Android a few weeks ago, so I don't know if I should even be doing things this way or if there are better solutions. Currently everything works, but I'm not sure if I'm going down the right path. I did read this, but wanted a more specific way on how to prefetch data.
This sounds like a good use-case for Volley.
[1] - After you have done the initial fetch you can create your Volley RequestQueue and fire off a bunch of Requests (String/JSON) that you can then parse / cache the result as you already do. Volley is asynchronous, so you do not need to worry about where you add them to the queue, but the callbacks are performed on the UI thread so make sure they are reasonable (they may cause animation jank)
[2] - See above.
I need to create an architecture design for my application.
App Details: In my app, i show some data which is fetched from server. So basically when you start the app, you get a list of categories and when you click on a category, application calls a web service using REST and get a JSON response. After getting the response, JSON data is parsed to create an Arraylist of objects and finally an adapter shows this data in a list view.
Package structure which i have thought of:
com.app.activities: contains all the activities required in the application.
com.app.customviews: custom views required for the application.
com.app.adapters: different list/grid adapters to create different types of list and grid views.
com.app.parsers: contains all parser classes to parse the JSON response received from the server. These classes basically will return an arraylist to the activities which will be further used by adapter class for creating list and grid views.
com.app.utils: contains functions which are used through out the application like function for getting the response from server for a request, getting a string from the inputstream, download an image from a certain url, logging etc.
com.app.model: contains all the model classes for various user-defined data types.
App work flow: When a certain category is selected, activity gets the response from utils and send it to parsers to get an arraylist of Model type. Now this arraylist is passed to the adapters which returns an adapter object which is finally used in showing the list/grid in the activity.
Now as per the application architecture, your code should be divided into following three layers:
Presentation Layer
Business Layer
Data Layer
Now i need to know, as per my application which part belong to which layer.
Please help, i am totally clueless about this.
thanks!!
Update: While googling i stumbled upon this link:
http://apparchguide.codeplex.com/wikipage?title=Chapter%2019%20-%20Mobile%20Applications
It says, your application should have some workflows, business components, entities etc.
So, i think my current package structure is incorrect as i am doing most of thing in Activities only.
So now, my question is: If i follow this architecture, what should be package structure or how do i setup my code base.
Is your app is only dedicated to presentation? The business layer is implemented in the server, since you do not change data, only show it.
For me model is in the data layer, all the rest is presentation.
your UI of application is, you can say it presentation layer. In business layer you do operations with REST and JSON web service. And the data layer resides on server. So basically you display the listview(Presentation) by using some services(Business) to get result(information) from server(Data).