How should i use data that i fetch from webserver, in android - android

I try to show data which i fetch from rest webservice (spring), in list(RecyclerView). Data can consist of thousands of rows. How should i use these data ?
should i fetch all data(or 100 rows ) and store them in local and read them from local to view in list. if i use these , how should i refresh local data ?
should i fetch 20 rows and view them , when user scroll down and arrive last item , i fetch another 20 rows ?
or should i use another way ?
If i store all data as array , it can throw outofmemoryexception . are there any tutorial or key words to search ?
How twitter or instagram use these data ? There can be a lot of items in twitter list ,but it doesn't crash and twitter can show items which downloaded before, offline(it means store data in local ,isnt' it ?)

To make app work smoother with RecyclerView, use Endless Scrolling.
Load 20 items at a time, apply pagination on your server API call.
Cache every response from server to local storage using OKHttp Response Caching.

Create a local database, store all data you already loaded inside the DB and add a "freshness" like a timestamp if your data is subject to change. If your data items are huge, consider loading an "index" of your data first, then load the individual details later.

Definitely you should not fetch all the data at once. Read about Pagination for dynamic data in android. Recycler View also uses inbuilt pagination for static data but in case of dynamic data i.e. when you are retrieving data from your api, you should send requests to your api when the user scrolls down the list to fetch the next set of data. In order to do that you'll have to extend RecyclerView's OnScrollListener and override the OnScrolled method.

Related

How to download certain amount of JSON using volley

Is there any way to download a certain amount of rows from JSON?
I have a MySQL database converted to JSON.
The JSON data is large and more than 1000 rows. obviously, i cannot download and parse all of them into my SQLite database in android at once.
I need to download a few rows and load more on Recyclerview scrolling. How can i tell volley to load a few of JSON rows not all of them?
Use LIMIT while detching the Data using SQL Query for fetching Data from MYSql Server.
SELECT * FROM your_table WHERE your_condition LIMIT number_of_rows;
This will Return only Return number_of_rows rows while fetching Data.
SELECT * FROM your_table WHERE your_condition LIMIT starting_row,ending_row;
This will Return only Return n number of rows starting from starting_row to ending_rowwhile fetching Data.
With pagination you can achieve this. Basically at server side you create API which will return some set of data like 50 set or 100 or may be more than that, So whenever you will load that set in your ListView or RecyclerView and on click of Load More or scroll end you call same API again to fetch other set of data and load in app.
If you are using Volley then use same logic to achieve it

How can i correctly access the data in my database using room library

List<Game> gamesList = db.gameDao().getAll();
I am trying to iterate through the list and make use of the data in the database, the command successfully receives the rows but how can I use the content of each row
You got the List already. now you can loop through and get the data like this
for(Game singleGameItem: gameList)
singleGameItem.memberObj

Load more Items in the middle of recycler

I'm having trouble building a twitter style load more button in the middle of my recyclerview. I'm currently working with a syncAdapter to fetch new data before inserting them into a db. I also have a ContentProvider and i'm using a Loader which is a CursorLoader to update the recycler_view's adapter.
When the syncAdapter fetches newer data than what exists in the app and there is more data available between the last of the new items fetched and the previous top item I want to show a LoadMore button in between.
My would-be logic for activating the loadMore button in my adapter is if the last row id of the newly fetched data set doesn't exist in the db. But the problem is I don't know where to perform this logic.
I can't make it in onLoadFinished because it is called after the new data is inserted, so I can't check the incoming data against the old data there, because by then the id of the last row in the incoming data set has already been written to the db.
I've thought of making the check while unparsing the incoming json before inserting to the db. I can check if the last id received exists in the db there, and know if a load more button is warranted, but letting the adapter know from there seems non-trivial. Since data is fetched on another thread(syncadapter or a service I've got), i'd have to write the fact that loadmore exists between two rows in SharedPreferences or send a broadcast. Is there a more elegant way of doing this, maybe i've missed something.
I'm open to suggestions.

Custom List view adapter only showing 100 entries

I didn't use parse query adapter, instead I made a custom adapter and passed all the text and images in that but Parse only loads 100 items. Can anyone tell me how to get to the next page of the database without using the parse query adapter?
This is a well known problem, the default limit is 100, you should use (saying that you have a query which is a ParseQuery object), you need to call the query.setLimit(int N) where N represents the number of results that you want returned .

Display SQLite query result in Listview in Android

In my Android app I have an activity with a listview that displays about 4000 items that are stored
locally in a SQLite Database. If I make an edit to an item, how can I get only this change in the listview,
without having to refresh all the item list (which means a new query for 4000 results)? This query slows down the performance.
I would like something like NSFetchedResultsController of ios.
Strategy should be -
As you are editing the contact, if the update is successful you just fetch the latest info from db for this specific contact only.
Update the edited contact object in your adapter's source List/Array's specific position.
Invoke your adapter.notifyDataSetChanged().
you must have an id for each contact in your SQL lite database.
when you edit a contact update that specific record in your database on basis of the id.
if update is successfull means the information you sent to database is stored successfully .
now you can use the same informtion to update your ArrayList/HashMap whatever you are using to populate your listview.
Ex:- suppose you edited 3rd index contact in your listview on successfull update you add like yourarraylist.add(3,contact);
and the fire notifydatasetChanged.
Try these steps if possible:
Try to fetch the data from db, but do it in different thread which won't effect the main UIThread.
Then you can call the adapter.notifyDataSetChanged() on adapter object. It will do the job i hope :).

Categories

Resources