The last time we introduced the Delete Feature and the dialog dialog box for Eatwhat, today we use SQLite for local data storage.
First, we define a SQL helper class Shopinfoopenhelper inherit Sqliteopenhelper.
public class Shopinfoopenhelper extends Sqliteopenhelper {public Shopinfoopenhelper (context context, String name, Cursorfactory Factory, int Version,databaseerrorhandler ErrorHandler) {Super (context, name, factory, version, ErrorHandler)//Todo auto-generated constructor stub} @Overridepublic void OnCreate (Sqlitedatabase arg0) {//TODO auto-generated method stub} @Overridepublic void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {//TODO auto-generated Method stub}}
In the constructor method Shopinfoopenhelper, configure the context environment, database name, version number cursor factory default setting NULL.
Public Shopinfoopenhelper (Context context) {Super (context, "shop_info.db", NULL, 1);}
CREATE TABLE Shopinfo: Primary key _id, field Shopname in OnCreate.
public void OnCreate (Sqlitedatabase db) {db.execsql ("CREATE Table Shopinfo (_id integer primary key AutoIncrement, Shopname char (20)) ");
The database and auxiliary classes are declared in Mainactivity. Declare the Createoropendatabase () method in the Init () method.
Database Creation Private Sqlitedatabase db;//Openhelper Auxiliary class private Shopinfoopenhelper mopenhelper;
The method body initializes the helper class Openhelper, creating or opening the database:
private void Createoropendatabase () {mopenhelper = new Shopinfoopenhelper (mainactivity.this);//If the database does not exist, create a writable database. If created then directly open db = Mopenhelper.getwritabledatabase ();}
Declaration Method Addlocaldata (shop shop):
private void Addlocaldata (Shop shop) {//stored mechanism, can only store basic types of data, such as String, int, and so on. Contentvalues values = new Contentvalues (); Values.put ("Shopname", Shop.getname ());
Insert Data Db.insert ("Shopinfo", null, values) in Shopinfo;}
Call this method in the logic of adding a button:
List Shop to add the store name Shops (addname); Add a newly instantiated shop object Shoplist.add (shop); Refreshes the ListView shopadapter.notifydatasetchanged (); Add to Local Data addlocaldata (shop);
Test results, the real machine test needs root after the Rootexplorer and other software to find, path: data/data/package name/databases/database name/table name
Eatwhatapp Development Combat (V)