Processing dynamic list view items efficiently in Android - 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.

Related

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.

Usin SimpleCursorAdapter confusion

I have a ContentProvider implementation in my app which works fine. I have a table called elements where the user can store a bunch of information.
What I am doing is when the user opens the app, I pull this data out of the database, process it using the display options set by the user (Like change the string formats, time formats, date formats, number decimals etc), and then put it in ListFragment which using my own implementation of ArrayAdapter. User can of course change the preferences in the middle of the session, where I reload the data and reformat it and present it to the user. The user can click on an item in the list, and see more details of that item. I accomplish this by overriding ListFragment.onListItemClick().
I have been reading about SimpleCursorAdapter. I am confused if the use of this would be more correct than using an ArrayAdapter for what I am doing. I am confused because I am not directly mapping the database data to the view. So should I be using a SimpleCursorAdapter? Also, the _ID column seems to be a requirement. I don't want to rename my table at this point. After a few articles and tutorials, I am not sure what to do. So any suggestions are appreciated.

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.

Async task with SimpleCursorList (Android sql select)

I'm relatively new to Android but I just cant google this. I have following situation:
quite large SQL db on android (need to select and load about 2000 records to ListActivity)
I use SimpleCursorAdapter so far BUT... it doesn't allow me to load data asynchronously with AsyncTask (SimpleCursorAdapter has no "add()" as e.g. ArrayAdapter does)
I know how to make it work with ArrayAdapter but then I lose the ID attribute every time the time is clicked and I want to do it the "clean" way and keep the id (not save it some place hidden)
===> For now user has to wait till all db output is parsed into GUI, it takes some time. How can I fix it to make it run faster ? I need something like SimpleCursorAdapter.add(item) or extend it but not sure ...
thnx
You should consider having some pagination mechanism, not loading everything in an ArrayAdapter but better, returning a simpleCursorAdapter with just a subset of size N of your records. When the user will reach the last row, display a button to increase N and refetch the data from your database.

Populating ListView with ORMLite

I need to populate ListView with List of objects returned from my Dao object.
The items get returned after 3 seconds, obviously to much time for the user to wait...
I'm using BaseAdapter as the ListView adapter.
2 questions:
How can get rid of the 3 seconds waiting time? Should I just retrieve the entire list of objects in a seperate worker Thread and display dialog in the meanwhile? Is there any mechanism that allows me to get the first, let's say... 20 records, display them and fetch the rest of the records while the user scrolls down the list?
If I would use cursors, rather than ORMLite, the list would then query the DB as the user scrolls down the list, releasing the objects of the hidden cells and the cells themselves, and not keeping all the objects of the cursor in the memory. How can I achieve this behavior with ORMLite?
I hope I was clear enough, despite the bad English ;)
Thanks.
You might want to load the data in an AsyncTask, and display a ProgressDialog while it loads. Lot of Android apps do this.
Cannot OrmLite return a DataProvider instead of the while list? (I too wanted to look into ORM on Android but the management decided against it "Its slow", but I still badly want it)

Categories

Resources