Load more Items in the middle of recycler - android

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.

Related

How should i use data that i fetch from webserver, in 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.

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 :).

Processing dynamic list view items efficiently in Android

In my app, I have a button called "Contacts" that allows the user to select contacts to add to his or her friend list. When clicked, the user is shown a list view of all the user's contacts. Each list view row contains the contact display name, icon, email address, and a button. The name, icon, and email are all fetched using the ContactsContract class.
The problem I'm running into is that processing all of the user's contacts is very computationally expensive.
I tried two solutions:
In a CursorAdapter, I tried modifying the bindView method. I passed in a cursor that queried the user's contacts and in the bindView method, I executed an asynchronous call to my server to return the relation of the contact to the current user. i.e. if the contact had the app installed and was a friend, the button would display "remove". This call would determine the button type and set the appropriate onClickListeners.
This didn't work because it would cause the list to be very slow, not smooth, and feel very laggy.
I just tried loading the contacts and the server queries in some different thread. When it would finish loading, I would initialize a BaseAdapter with the the name, icon, email, and button type already determined in the different thread.
This didn't work because it took 30 - 40 seconds to load the contacts. However, it was much smoother once it finished loading.
How can fix this issue?
Should I get the data from the server first or pre-load it for each item in the adapter?
Edit:
The layout would look similar to this:
What i would do is to load both the contacts and the remote data, create a custom class to hold them, put it inside an ArrayList<MyCustomContactClass>, and only after its all ready i would load the adaptar into the listview.

Synchronizing db calls

I have a database schema where i am loading a bunch of clients from a server with data such as first name, last name and id into my db when the app starts. THis data is then displayed in a list format to the user. I have another tab in my app that switches to a view which shows the client list sorted by id into sections similar to the people app with section headers. The trouble is, the user can switch to the sorted list tab immediately upon startup and that list relies on the db already being populated with client objects. It would be making a query on an empty db if the user switches immediately. Is there any way to block that call until the db is fully loaded with the client data? I know java has synchronized methods, so can i sync on the db query or do anything like that?
The easiest is to add an isReady boolean and to loop it is true (assuming a seperate thread is populating the db). Alternatively you can disable the button until it is ready, which is a better solution.

Expandable ListView and cursor adapter

I have a ListView which uses cursor adapter to show the records from database. When a new records is inserted in database ,it works great and shows that entry on top of ListView on requery.
Now I am facing problem when User scroll down the List, background thread call web service and brings old data. In this case when it does requery, old data is also getting appended on top of list which should append old data at the end of list.
What should i need to do differently, to add old data at the end of List rather than top ? do i need to change method of insertion when I am getting this old data ?
I think you need to store a date field in your bd table and make an ORDER BY in the query....

Categories

Resources