Expandable ListView and cursor adapter - android

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

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.

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

SQLite. How to populate ListView partially?

I am trying to populate ListView from SQLite database.
I have this table:
CREATE TABLE meaning(
key TEXT,
value TEXT,
entries BLOB);
And created this index for column key:
CREATE INDEX index_key on meaning(key);
I am using this query to get needed data from database:
SELECT rowid AS _id, value, entries
FROM meaning
WHERE key
LIKE 'dog%'
ORDER BY key
LIMIT 100
Result is coming in 510ms. This is quite slow for incremental search. Is it possible to populate ListView part by part so that when user scrolls down ListView, other part of data will be shown?
Yes, you can load the data in parts so that the user sees the result set only as the view is being swiped down. Follow the example at this location http://www.avocarrot.com/blog/implement-infinitely-scrolling-list-android/ to see how it's done.
Basically all you're doing is listening to the scroll change and then adding more results from a loader. Don't forget that once the results have been added to the underlying list, they are not removed simply because you swipe back up. From that point on, they will always be there.

Android : Maintaining Database update in ListView Automatically

I am populating ListView with data from database . To bind data i'm using a CursorAdapter My list view looks like this
And my db will get updated through web service on every five seconds so when ever the db get updated i need to reflect the change on my list view . As you see my first 2 items in ListView row is directly bind with the columns in db it will get automatically updated. But other items are calculated in user side. so how will other items can be updated ??
Did you try calling Adapter_Object.notifyDataSetChanged() ?

Display search results from both local DB and remote service

I have an app that stores items in a local database, displayed to the user using a ListView, also in the layout is an EditText that can be used to filter the list. All of this works fine, my issue is I would also like to include items returned by a web service (JSON) in this list also. I'm not having an issue parsing the JSON, my issue is simply how do I insert/add the results from the web service to the ListView?
The data for the ListView comes from a cursor handled by a SimpleCursorAdapter, I just can not figure out how to add the items from the JSON results to the Cursor (I don't actually think you can write to a Cursor outside of performing a Query).
If you don't want to insert the JSON results into the local DB, then one approach would be to use an ArrayAdapter to back the ListView. You can then populate the array by inserting results from the cursor and the JSON query.
Hope this helps,
Phil Lello

Categories

Resources