SQLite and UI update order on android? - android

Hello this is a simple design question for better practices:
Example and Context:
I'm creating an app that shows a list of items in a players stash. For this i need following requests:
Ask for all items IDS, one request.
Look for each ID and inside each response i'll get the ICON link. As many requests as ITEMS here
Request each ICON. As many request at items here.
All this will be presented on a Recyclerview list with cardviews and saved locally to the device database.
The question is: In which order should i do this???
Option A: get items IDs > Save to DB > Read DB > get ITEM OBJECTS > Save to DB > Read Database > GET ICON IMAGES > FILL UI LIST
Option B: get ONE item ID > get ONE item Object > get ONE item icon > present to UI > save Item Object to Database
ON both cases i will have timeout or try cach for items that fail to get.
ON both cases in the future I will only request ICONs for existant items or full requests for new items in stash.
If you have another suggestion or approach feel free to add it.

If i was you, I would create the loading so that the user sees items without the user having to wait too long.
I would (asynchronously) do the following:
Get Item IDs
Request all items. When a request completes, add a item with a placeholder ICON to the UI
For each requested item, request the ICON. When a ICON request completes, update the UI and save it to database
While the objects are loading, you can show a spinning progress bar on top of the RecyclerView, so the user sees the progess bar spinning, and the items being added one by one in the background. You can even add an animation very easily to the RecyclerView, so your app looks even cooler while loading.

Related

MVVM : fetch data from server and notify item

I will take an example to explain my problem : in a search screen, I need to search for a big list of movies.
I can bookmark each movie, so I need to notify my item in my recycler, here is my problem.
Do I need to put every movies searched in room database and it will be simple after for the notify with MVVM & DiffUtils?
Because, when I used MVP, I created a small database with MovieId and a boolean for bookmark, and when the user bookmark a movie, I run through in my items in adapter and notify the concerned item. But this solution is very ugly now with MVVM.
Thanks in advance
Just for the search screen you don't need to save data to local database. Instead of that you can just bind one list to RecyclerView and update it with another list from API using DiffUtils.

how to fetch 20 objects at a time from 1000 objects and show in listview

In my application I am fetching the data from a web service in XML format, and parsing it and showing the data in listview. The problem is that if the web service contains 5000 objects then it takes a lot of time to display the data.
Can it be possible to show some data in listview and fetch the data at the same time at the end of the list.
Please provide me some sample code.
If you use convertView in your ListAdapter´s getView method it should not matter how many items you have in the list since the Views are beeing reused.
If your Listadapter takes an array of som sort you could add items to the array continuosly and call
mListAdapter.notifyDataSetChanged();
every time new data is added to the list.
By Using AsyncTask you can do this easily as each object is being fetched can be shown in listview using publishProgress() method while also updating user about what percentage of data hasbeen loaded.
Update:
By the way according to your situation the tool below which is developed by commonsware https://stackoverflow.com/users/115145/commonsware will suits you best...
https://github.com/commonsguy/cwac-endless
cwac-endless: Provides the EndlessAdapter, a wrapper for an existing ListAdapter that adds "endless list" capability. When the user scrolls to the bottom of the list, if there is more data for this list to be retrieved, your code gets invoked in a background thread to fetch the new rows, which then get seamlessly attached to the bottom of the list.

How to update ListView on scrolling while retrieving data from server in Android?

Currently, I'm using AsyncTask to handle Http connection and retrieve data as JSON format.
Loading all data is trivial but it consumes too much time, so I decided to switch to load 10 items at a time using LIMIT OFFSET (mysql).
Next I set up the event onScroll for my list view to create a new AsyncTask each time user scroll. However, from what I read, AsyncTask is stored in a thread pool which is limited 5 threads at a time, so I'm not sure this is a right approach. I'm newbie to client/server app, so could I anyone give me an advice on this issue? Any related article, documentation would be greatly appreciated.
Here are few useful links for it,
Android: Implementing progressbar and "loading..." for Endless List like Android Market
Endless Listview with current Async Task
Android Endless List
http://www.androidguys.com/2009/10/21/tutorial-autogrowing-listview/
http://mylifewithandroid.blogspot.com/2010/03/progressively-loading-listviews.html
In simple steps,
As user scrolls – detect the end of the list 1)Display a progress
notification 2)Ask for update 3)Receive update (asynchronously) and
extend list
A typical approach would be e.g. to load 25 initially and then have a footer in the list that displays e.g. the current count and the total count and upon pressing loads another 25 and so on. That would be a paged sort of loading.
When you do that you have to keep the current position and notify the adapter that the list has changed.
If you are using a ListView, I believe I can safely assume that you must be using some sort of ListAdapter. Instead of starting a new AsyncTask in the onScroll event, you should maintain just one single AsyncTask to retrieve data from the server, add that data to the ListAdapter dataset and then call notifyDatasetChanged on the ListAdapter.
The ListAdapter and ListView will take care of the rest.

AsyncTask in onItemClick() & configuration change

I load a list of some data. I do it in AsyncTask and I also use lastNonConfigurationInstance, so when I change the configuration of a device, list is not loaded again. Everything is clear here.
BUT when I click on an item, the "extended" data of this item is loaded. And here I'm not sure what to do... Should I use another AsyncTask for downloading data of the item or just do it directly in onItemClick()? If I put it in AsyncTask I should also remember about configuration change and in this case I should also use lastNonConfigurationInstance?
Thanks in advance for all your suggestions, answers...
If you "extended" data taken more than 20ms to fetch, you should consider using AsyncTask for this too.
If your data is quick to fetch, you don't need lastNonConfigurationInstance. Seems like you could just save you "current index" of the item the user clicked on in the onSaveInstanceData. When your activity is recreated in onCreate(Bundle) read the selected index from the bundle and load data for the sub item.
If your extended data takes time, then you could use the lastNonConfigurationInstance mechanism to save data for the currently selected item.

Android Toast Notification Question

Using the HelloListView example it provides code for making a quick toast notification based on what the user clicks on in your application. I have a list I display that when an item is clicked I would like to display associated information.
My application displays a List of Tasks (custom class) by subject in a very simple ListView.
The ListView is just 1 TextView object with the Tasks Subject as it's content.
When a task's subject is clicked I would like to show other information about that task. To keep it simple the task should have an AccountNumber associated with it (integer value). I would like to display that.
The most direct method to do this I think is to display my list of tasks by subject then when a user clicks on a subject I could search my Task List for a task that has that subject and pull out the account number then. This has it's holes of course. Most obviously what about tasks with the same subject?
Any tips?
Some more information on my code:
Classes:
Task - A single task, has things like start date, end date, subject, account number of the client the task is for
TaskList - A List of Task objects with functions wrapped supporting a list of Task objects
I display a list of tasks in a ListView. Right now I am only displaying a task's subject. I want a toast notification to pop up the account number of the Task object they just clicked on.
Override the list adapter class. Save all your 'tasks' as objects and just display the portion ('subject') you want when the adapter is queried for a specific item. Then you can trap the onClick like you do now and display whatever segment of the corresponding object you want.
EDIT: Its explained better here.

Categories

Resources