SQLITE DATABASE is a data btsan that is designed to store a large amount of data locally in Android.
In the last article, we learned to work with Recylcerview and fill it with various elements. Now let's fill the recylceView data from the database.
This is a database code. At first glance, it looks difficult, but in fact everything is much simpler than it seems. Perhaps you will not need the whole code, since below I give functionality much wider than we will use now.
Database class
Public Class MessengerDatabase Extends Sqliteopenhelper {Private Static Final String Database_name = "DB"; // name BDPrivate Static Final String Table = "Messengertable"; // Table name. There can be several tables in one database, but it is better not to do this. Private Static Final intabase_version = 1; // version of the database
// названия колонок в таблицахprivate static final String ID = "id";private static final String NAME = "name";private static final String MESSAGE = "message";private static final String PHOTO = "photo";// создаем бдpublic MessengerDatabase (CONTEXT CONTEXT) {SUPER (CONTEXT, DATABASE_NAME, NULL, DATABASE_VERSION); } // Create a table with the right names of the columns. @VERRIDEPUPUPULIC VOID Oncreate (SQLITEDABASE DB) {String Create_table = "Create Table" + Table + "(" + ID + "Integer Primary Key," + Name + "Text," + Message + "TEXT," + PhOTO, " + PhOTO," + PhOTO, " + PhOTO," + PhOTO, " + PhOTO," + PhOTO, " + PhOTOO," + PhOTE ) "; db.exexql (Create_table); } @VERRIDEPLIC VOID Onupgrade (SQLITEDABASE DB, Int overSion, Intversion) {DB.execsql ("Drop Table IF Exists" + Table); oncreate (DB); }
// save data in the database
Public VOID Save (itemData itemData) {
// Create a variable of the database sqlitedatabase db = this.getwritabledatabase (); ContentValues Values = New ContentValues ();
// save data in values, and then in the database itself values.put (name, itemdata.name); values.put (Message, itemData.message); values.put (Photo, itemdata.photo); db.insert (Table, Null, Values); db.close (); }
// Getting data one item
(in our case of a person) Public itemData getitem (int ID) {sqlitedabase db = this.gethedAbledatabase (); Cursor Cursor = Db.query (Table, New String [] {Id, Name, Message, Photo}, Id + "?", New String [] {string.valueof (id)}, null, null, null, null ); if (cursor! = null && cursor.getcount ()! = 0) {cursor.movetofirst (); ItemData itemData = New itemData (); itemData.setid (cursor.getint (0)); itemData.setname (cursor.getstring (1)); itemData.setmessage (cursor.getstring (2)); Return itemData; } Else return new itemdata (); }
// Obtaining all data the form of the ARRYALIST list
Public Arraylist
// Update/Change data of one item by ID
Public Boolean Update (ITEMDATA ITEMDATA) {SQLITEDABASE DB = this.getWritableDatabase (); ContentValues Values = New ContentValues (); values.put (name, itemdata.name); values.put (Message, itemData.message); values.put (Photo, itemdata.photo); db.update (Table, Values, id + "=?", New String [] {string.valueof (itemData.getid ())}); db.close (); return true; }
// Removing data of one item by ID
Public Void Delete (itemData itemData) {sqlitedatabase db = this.getwritabledatabase (); db.delete (Table, id + "=?", New string [] {string.valueof (itemData.id)}); db.close (); }}
The Mainactivity file will now look like this:
Public Class Mainactivity Extends Appsompatactivity {Arraylist
// We get data from the databases of the MessengerDatabase MessengerDatabase = New MessengerDatabase (GetapplicationContext ()); Dataarraylist = MessengerDatabase }}
In the same way, you can use all other methods of our database:
ItemData = messengerdatabase.getitem (ID);
messengerdatabase.save (itemData);
Ссылка на предыдущий урок по RecylcerView -