zoukankan      html  css  js  c++  java
  • Android网络编程之一个Android下菜单系统模块的实现(客户端—开桌功能(上部))

    所谓开桌功能,就是由操作员录入待开桌号与客人人数,把这些信息传送到数据库服务器端的ordertbl表中,而为了减轻服务器端负担,我们将关于桌号等一些信息的表存入客户端的sqlite数据库中,方便客户端读取。而对本地sqlite数据的操作则借助于我们自己编写的ContentProvider类实现。由于有一定的难度,我们把这部分分为上下两个部分,本部分先讨论本地Sqlite的建立,ContentProvider的一些准备工作。

    在项目Wireless的com.moka.provider目录中,新建一个元数据接口Tables继承BaseColumns,

    public interface Tables extends BaseColumns {
        
         // 注意,此处AUTHORITY一定要和Manifest.xml中的配置完全相同
         public static final String AUTHORITY = "com.moka.tableprovider";
         // 表名
         public static final String TABLE_NAME = "TableTbl";
         // 访问本表所需的URI
         public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
         // 字段名
         public static final String NUM = "num";
         public static final String DESCRIPTION = "description";
         // 排序操作
         public static final String SORT_ORDER = "num DESC";
         
    }

    然后新建一个DBHelper类继承SQLiteOpenHelper,主要负责初始化的时候建立存储桌号等信息的数据表

    一些知识点DBHelper的onCreate方法只在第一次建立数据库后并且调用getReadableDatabase()时才会调用,onUpgrade()方法只在版本号变化时调用。

    public class DBHelper extends SQLiteOpenHelper {
        
        private static final String DATABASE_NAME = "Wireless.db";
        private static final int DATABASE_VERSION = 1;
        
        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + Tables.TABLE_NAME + " ("
                    + Tables._ID + " INTEGER PRIMARY KEY,"
                    + Tables.NUM + " INTEGER(11),"
                    + Tables.DESCRIPTION + " TEXT"
                    + ");");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_NAME);
            onCreate(db);
        }
    
    }
    View Code

    然后来写ContentProvider:

    关于ContentProvider的更详细内容,可见http://www.cnblogs.com/moka/archive/2013/05/05/3060708.html,这里与它大同小异。

    public class TableProvider extends ContentProvider {
        
        private DBHelper helper = null;
        private static UriMatcher uriMatcher = null;
        private static final int GET_LIST = 1;
        private static final int GET_ITEM = 2;
        static {
            uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
            uriMatcher.addURI(Tables.AUTHORITY, Tables.TABLE_NAME, GET_LIST);
            uriMatcher.addURI(Tables.AUTHORITY, Tables.TABLE_NAME + "/#", GET_ITEM);
        }
        
        @Override
        public boolean onCreate() {
            helper = new DBHelper(getContext());
            helper.getReadableDatabase();
            return true;
        }
        
        @Override
        public String getType(Uri uri) {
            // 暂时不需要查看类型
            return null;
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            SQLiteDatabase db = this.helper.getWritableDatabase() ;
            long id = 0 ;
            switch(uriMatcher.match(uri)) {
            case GET_LIST :
                // 插入数据操作
                id = db.insert(Tables.TABLE_NAME, Tables._ID, values);
                String uriPath = uri.toString() ;
                String path = uriPath + "/" + id ;
                return Uri.parse(path) ;
            case GET_ITEM :
                return null ; 
            default:
                throw new UnsupportedOperationException("Not Support Operation :"
                        + uri);
            }
        }
    
        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
            // 获得可读数据库
            SQLiteDatabase db = helper.getReadableDatabase();
            switch (uriMatcher.match(uri)) {
            case GET_LIST:
                return db.query(Tables.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
            case GET_ITEM:
                long id = ContentUris.parseId(uri) ;
                String where = "_id=" + id ;
                return db.query(Tables.TABLE_NAME, projection, where, selectionArgs, null, null, sortOrder);
            default:
                throw new UnsupportedOperationException("Not Support Operation :"
                        + uri);
            }
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection,
                String[] selectionArgs) {
            // 暂时不需要更新操作
            return 0;
        }
        
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            // 暂时不需要删除操作
            return 0;
        }
    
    }
    View Code

     注意在Manifest.xml中的配置,出一点儿错都不行:

    <!--authorities一定要与元数据接口中的完全相同--> 
    <provider 
      android:name="com.moka.provider.TableProvider"
      android:authorities
    ="com.moka.tableprovider"
    />

    然后第一次启动时,表就会建立起来,为了方便,我们预先操作了ContentResolver向sqlite中的tabletbl表中插入了10张桌的信息,关于查看sqlite中的数据,推荐使用SQLiteSpy,导入db文件查看非常方便

    我们来看看此时TableTbl的内容:

    在下一篇中我们会讨论OrderActivity的操作

  • 相关阅读:
    CSS3_02之2D、3D动画
    CSS3_01之选择器、Hack
    css_04之显示、定位
    css_03之文本、浮动
    css_02之盒模型、渐变
    javascript获取非行间样式的方法(可以兼容各个浏览器)
    gulp的用法
    grunt的用法
    grunt
    git用法
  • 原文地址:https://www.cnblogs.com/moka/p/3078225.html
Copyright © 2011-2022 走看看