Android: ListFragment, no data and data is loading situations - android

I have got ListFragment in my application.
And I got 2 possible situations:
When data is loading (from internet or database)
When data count is zero
So, what is the best way to show that to user?
I have tryed to use #android:id/empty but it is showing in both of situations. I can not control its behavior.

My suggestion would be:
1. Spinning progressbar while the data is loading
2. a short toast stating "No data" or something meaningful if the list is empty.

Related

data syncing with server to room database

I am new to android. In my android app, I showing the products list. Now I can sync all the products with a single API call. In slow networks, the app getting broken with the timeout error. I want to sync the products by chunks of data and I want to show a loader also. How can I achieve this?
Thanks in advance.
You're looking for something called Pagination. You can get a specific list/chunk of products from your API with a specified 'page' parameter. The 'page' parameter usually starts off with a value of 0 (page=0), then when the user scrolls to the bottom of the list, you increment the 'page' parameter and get the next chunk of data (page=1), So on as the user scrolls to the bottom of the list.
To show a lading to let the user know of data being loaded from the API, simply use a progress bar that can be shown whenever the API call is in progress and hidden when the API call is finished.

How to create offline first RecyclerView in Android? And how to update only the latest data?

How to create offline first RecyclerView in Android so the users still can see the list when they don't have any internet connection?
I already know how to get parse JSON and populate it in RecyclerView, the problem is, users still can't access it when offline. And if I use cache using SQLite, how to update only the latest data from the server?
for example, I already got 3 data from server and populate it in RecyclerView like this:
Data 3
Data 2
Data 1
After that, there is an update from the server so there are 4 data from the server. I only want to add the 4th data without load from the 1st. And its going to be like:
Data 4 --> only add this to the RecyclerView without reload past data
Data 3
Data 2
Data 1
Regards,
Elmer
I have done something similar in this project:
https://github.com/isaacurbina/MyMovies/blob/master/app/src/main/java/com/mobileappsco/training/mymovies/Fragments/ResultsFragment.java
Basically, I use an AsyncTask to load data as the user scrolls down, loading more content (kind of like 9GAG or Pinterest do) managing a "page" counter.
Then, as I receive the data, I join it to the List object using list.addAll(results), being results another ArrayList<> of the same kind.
Then you can use adapter.notifyDataSetChanged() on your RecyclerViewAdapter and it will add them super fast, or you can use an animation to show them being added slowly.
I hope it helps, kind regards!

Proper way of using ResultNotifier.ShowProgressDialog in App Inventor 2?

Expanding the functionality of an under development app, I need to show to the user a progress notification dialog. Problem is, I cannot get it done right. Furthermore, I cannot dismiss this notifier properly. Have tried with a clock and a variable set to e.g. "5000ms" and then to "0", without any lack.
What I need to achieve is the following functionality:
a. Check if the tag "storeparsedData" is in a TinyDB, populated with the fetched JSON data. I have this done, following #Taifun advice in my relative question.
b. If the tag is not there (empty list), do a getWeb.gotText block to get the JSON data (this is done with procedure "getWebData". This functions right, but takes a while about 1'35'' or more, so need to show something to the user.
c. While fetching JSON data form web, need to show a "ShowProgressDialog" notifier to the user, so I can cope with the smartphone being seemingly freeze.
d. If the tag "storeparseData" is populated with fetched JSON data, dismiss the notifier.
Have tried the following coding, without relevant success:
Can someone help me out, to achieve this functionality in this app? A blocks code or something to follow and learn, will be awesome.
Thank you all in advance for your answers.
[Edit1]
After #Taifun suggestions, the functionality in question seems to be working, but there is a problem."ShowProgressDialog" block never fires, neither on device or companion. Also where should block "DismissProgressDialog" be attached to disable notifier upon JSON data received?
Here is the reviewed blocks code, for checking stored tags in TinyDB. "ShowProgressDialog" never fires as it should. Are there any suggestion for this issue?
Here is the blocks code for the getWeb function to get the JSON data:
Please advise, with a block code if applicable.Thank you all.
Your progressNotifier.AfterChoosing event never will fire, because that event only fires after choosing something from a Notifier.ShowChooseDialog block, but not for Notifier.ShowMessageDialog blocks. Therefore use a Notifier.ShowChooseDialog block instead and set the second button in that block to empty string.
Your while loop will freeeze your app, as you already realized... You do not need the Clock.Timer event at all to check if your data is there.
Just do it like this: after having received your data in the Web.GotText event and having stored the data in TinyDB, then dismiss the progress dialog and display the message "Database is ready".
Update: Instead of storing your list n times inside the for each in list loop, you should store it only once after the for each in list loop is finished... Same for the DismissProgressDialog and ShowAlert block...
What is the purpose of that join block? You might want to remove it...

SimpleDB - how long after insert until item is available to be read?

I have a Fragment, and once the user presses OK, an Item is added to my database and its ID is added to the ArrayAdapter. Immediately after, the adapter tries to draw the view, but the first time it tries to get its attributes, it returns a null HashMap (it gets drawn properly the following times).
Is there a way to make sure the item is in the table before trying to get its attributes?
Even putting the attribute retrieval into a while loop until it returns a not-null HashMap doesn't work so it doesn't look to be an issue of time.
You need to do Select or GetAttributes with ConsistentRead=true as Amazon SimpleDB supports two read consistency options: eventually consistent read and consistent read. Eventually consistent read is default. For more detail please refer doc. link
Try using AsynTask.
Add item to database in doInBackground.
Read it in postExecute.
You are done.

Downloading data in separate thread before initializing a ListView

I'm having issues with multithreading in my application. I know there are many posts on Threads/AsyncTasks/etc, but none seem to address my specific problem.
Basically, I get a query string in my search Activity, then send it to my results Activity, where the string is used as a SQL query, the results are returned as an array of JSON objects, then I display these objects in a ListView (which is part of the results Activity). All of my SQL connection and retrieval is done in a separate class that I call at the start of the results Activity.
MySQLRetrieve data = new MySQLRetrieve();
ArrayList<Tile> tiles = data.getResults(nameValuePairs, isLocationSearch);
The above code is how I get the SQL response and convert into an ArrayList, which I then use the populate my ListView with. getResults() takes care of all of this.
I already have separate threads working to download images into the ListView, but what I can't get to work is getting the SQL query and result to run in it's own Thread. What I want to achieve is this:
User enters search query in search Activity.
Intent is sent to results Activity, and it starts immediately.
ProgressDialog (just the animated spinner thing, not a loading bar) displays while the SQL query is taking place.
ListView populates with objects from the JSON array, lazy loading images as they come.
I have steps 1,2, and 4 working well, but 3 is the problem. I've looked up AsyncTasks, which seem to be the answer, but I just can't get them to work. Does anyone have a solution to this problem? I need to do this, so when starting the results Activity, the UI changes immediately to the results Activity and doesn't have to wait until the SQL response is returned.
And yes, I've already read the painless-threading post.
Thank you.
I would recommend against creating that ArrayList<Tile> to reduce memory consumption (and code size) and instead directly bind the SQLite Cursor to the ListView using a CursorAdapter.
That alone might just increase the performance enough that you don't need to do any async loading.
If you still want async loading, check out the LoaderManager framework (available since Android 3.0/ API level 11, with Android support package down to 1.6/4) which will automagically do asynchronous loading of your Cursor -- either using the built-in CursorLoader (if you happen to have a ContentProvider), or the SimpleCursorLoader created by a fellow SO user (if you don't).

Categories

Resources