Eatwhatapp Development Combat (V)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.