Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 999
Fivos Avatar asked Mar 25 '26 21:03

Fivos


2 Answers

Strategy should be -

  1. As you are editing the contact, if the update is successful you just fetch the latest info from db for this specific contact only.
  2. Update the edited contact object in your adapter's source List/Array's specific position.
  3. Invoke your adapter.notifyDataSetChanged().
like image 194
Amit K. Saha Avatar answered Mar 30 '26 08:03

Amit K. Saha


  1. you must have an id for each contact in your SQL lite database.
  2. when you edit a contact update that specific record in your database on basis of the id.
  3. if update is successfull means the information you sent to database is stored successfully .
  4. 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);

  5. and the fire notifydatasetChanged.

like image 35
Basant Kumar Avatar answered Mar 30 '26 08:03

Basant Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!