![]() ![]() This tutorial will introduce you to the use of SQLite databases within an Android application by building an application to track assignment deadlines The tutorial is designed to walk you through developing the project from scratch, but, if you have limited time and only want to focus on the database aspect of the tutorial, then you can download this starter project, and skip the bits of the tutorial which are already in place. We can change the cursor to update the adapter at any time with: // Switch to new cursor and update contents of ListView todoAdapter. This will then trigger the CursorAdapter iterating through the result set and populating the list. lvItems ) // Setup cursor adapter using cursor from last step TodoCursorAdapter todoAdapter = new TodoCursorAdapter ( this, todoCursor ) // Attach cursor adapter to the ListView lvItems. Now, we can use the CursorAdapter in the Activity to display an array of items into the ListView: // Find ListView to populate ListView lvItems = ( ListView ) findViewById ( R. rawQuery ( "SELECT * FROM todo_items", null ) Attaching the Adapter to a ListView getWritableDatabase () // Query for items from the database and get a cursor back Cursor todoCursor = db. Once you have a database and tables defined, then we can get access to a Cursor by querying the database with rawQuery: // TodoDatabaseHandler is a SQLiteOpenHelper class connecting to SQLite TodoDatabaseHandler handler = new TodoDatabaseHandler ( this ) // Get access to the underlying writeable database SQLiteDatabase db = handler. This requires us to use a SQLiteOpenHelper for persistence as described here or an ORM that provides access to the underlying database. In order to use a CursorAdapter, we need to query a SQLite database and get back a Cursor representing the result set. Finally, we override the bindView method, which is used to bind all data to a given view to populate the template content for the item. Next, we override the newView method, which is used to inflate a new view template. The naive approach to this (without any view caching) looks like the following: public class TodoCursorAdapter extends CursorAdapter įirst, we define a constructor that passes the cursor and context to the superclass. To do this we need to override the newView method and the bindView method. Next, we need to define the adapter to describe the process of projecting the Cursor's data into a View. We can simply create an XML layout template in res/layout/item_todo.xml, representing a particular cursor row: Defining the Adapter When we want to display a series of items into a list, using a custom representation of the items, we need to use our own custom XML layout template for each item. ![]() To create this database table, we would use SQLite persistence or an ORM that allows us to define objects mapped to tables. In this case, we will define a database table called todo_items for a collection of todo items with a string body and an integer priority. Using a Custom CursorAdapter Defining our Tableįirst, we need to define a table within the database from which we will load our cursor. Which fields of the cursor to bind to which views in the template.Which layout template to inflate for an item.The CursorAdapter fits in between a Cursor (data source from SQLite query) and the ListView (visual representation) and configures two aspects: When we want the data for the list to be sourced directly from a SQLite database query, we can use a CursorAdapter. ![]() In Android development, any time you want to show a vertical list of items you will want to use a ListView which is populated using an Adapter to a data source. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |