Android - Automatic updating view when any change in database - android

I am developing an android application where it shows a list of name from database and when database values are changed (inserting/deleting item) then View should automatically reflect that change.
I don't want polling and Broadcast Receiver.I want things like binding ListView to database. is it possible?

Assuming that your database entries do have unique ids and you do have a reference to the ListView while performing database actions you could tag your views in the ListView with those ids at the same time you do it with the names. When performing an insert or delete operation of your database you run a refresh function which iterates through your ListView. If the updated id matches one of the tagged views you refresh the data inside the view.

Related

Populate ExpandableListView with data from SQLite in Android Studio

I'm developing android app and want to fetch some result from sqlite db inside the app and display it in "ExpandableListView". here is what i want. The app will provide some list of disease(problem) for the user to select and when a specific disease is selected, a query will get executed based on the selected disease and fetch 'TradeName' from db that have the same 'ActiveIngredients'
The ActiveIngredient will be the PARENT list and all tradeNames that contain the active ingredient will be the CHILD list.
Previously i have used to normal ListView to populate the results but when the data is populated redundant ActiveIngredients will be listed.
The expected output should look like this.

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

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() ?

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