Best Practices for Quick Communication between Data and the User Interface in Android - Adapter, Filter, CursorLoader and ContentProvider

Suppose we have an Activity with n TextView that are a single note. These notes are stored somewhere (local database, network, etc.), and each time onResume() is onResume() , the corresponding amount of TextView is drawn in accordance with this stored data.

Now, let's say a user wants to delete a note, what would be the best way to resolve a particular TextView back to its storage object?

At the moment, I only know with View.Tag and with some manager to translate it into a data object, but it looks pretty dirty.

Are there any other options?

+1
android android-sqlite android-adapter android-loadermanager android-cursorloader
Apr 05 '15 at 9:58
source share
1 answer

The Android Adapter has a bridge between the view and the data model. You can display n TextViews both ListView and GridView , and when a user adds or removes a note, the local or server database is first updated. Upon completion of the web service call and / or updating the local database, new data is added to the base Adapter . Then View updated by calling adapter.notifyDataSetChanged() . It will be a way to do it.

Approaches:

  • If you are updating a local SQLite database, you might consider using CursorAdpater to store data for the View , since it directly displays the records in the local database in the View .
  • If you use a ContentProvider , you can even combine a CursorAdapter with a LoaderManager and CursorLoader : they connect to the life cycle and Activity / Fragment monitor of the underlying ContentProvider for published changes automatically in the View in a separate thread.
  • You can also use Filter in conjunction with the Adapter to define a dynamic mechanism that sorts data records on the fly. Filtering is performed using Filter in a separate thread, as requested by the user, possibly in AutoCompleteTextView .

Literature:

  • See the Extracting Contacts list guide. This example retrieves a set of contacts from ContentProvider contacts based on a dynamic, alphabetical search by User. It uses CursorAdapter , CursorLoader and LoaderManager to monitor and update data, and it displays search results in a ListView .
  • See also an example of a real-time Android (instant search with a filter class ) that shows how to Filter .
  • Android AutoCompleteTextView with custom adapter filtering .
  • Android AutocompleteTextView using ArrayAdapter and filter .
+3
Apr 05 '15 at 10:23
source share



All Articles