Android网络编程之一个Android下菜单系统模块的实现(客户端—添单功能(上部))

接下来,我们完成客户端的添单功能。选择添菜弹出一个Dialog,选择菜品,输入数量与备注后,显示在下方的ListView中。

为了方便,我们仍选择在客户端本地建立sqlite菜品表,并为其编写一个ContentProvider

本篇依然先介绍数据库与ContentProvider等准备工作的编写。

在com.moka.provider包中,先定义关于菜品表的元数据:

public interface Menus extends BaseColumns {
    
     // 注意,此处AUTHORITY一定要和Manifest.xml中的配置完全相同
     public static final String AUTHORITY = "com.moka.menuprovider";
     // 表名
     public static final String TABLE_NAME = "MenuTbl";
     // 访问本表所需的URI
     public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
     // 字段名
     public static final String PRICE= "price";                // 价格
     public static final String TYPE_ID = "typeId";            //类型
     public static final String NAME= "name";                // 名称
     public static final String PIC= "pic";                    // 图片
     public static final String REMARK= "remark";            // 备注
     // 排序操作
     public static final String SORT_ORDER = "_id ASC";
     
}
View Code

然后在上个功能中已经写好的DBHelper类中的onCreat()与onUpgrade()方法中分别添加如下语句建立新表(注意要先把旧的.db文件删掉,因为onCreat()只在初次有效)

db.execSQL("CREATE TABLE " + Menus.TABLE_NAME + " ("
                + Menus._ID + " INTEGER PRIMARY KEY,"
                + Menus.PRICE + " INTEGER,"
                + Menus.TYPE_ID + " INTEGER,"
                + Menus.NAME + " TEXT,"
                + Menus.PIC + " TEXT,"
                + Menus.REMARK + " TEXT"
                + ");");
View Code
db.execSQL("DROP TABLE IF EXISTS " + Menus.TABLE_NAME);
View Code

 

再写MenuProvider,基本与开桌功能的provider一致,只是把元数据接口换成Menus:

public class MenuProvider 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(Menus.AUTHORITY, Menus.TABLE_NAME, GET_LIST);
        uriMatcher.addURI(Menus.AUTHORITY, Menus.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(Menus.TABLE_NAME, Menus._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(Menus.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
        case GET_ITEM:
            long id = ContentUris.parseId(uri) ;
            String where = "_id=" + id ;
            return db.query(Menus.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

注册provider:

<provider 
    android:name="com.moka.provider.MenuProvider" 
    android:authorities="com.moka.menuprovider"
/>

 

为了方便操作,先向menutbl表中插入5条数据如下:

 

OrderAcitvity中的余下操作在下篇讨论

posted @ 2013-05-15 18:11  来杯冰镇魔卡  阅读(331)  评论(0编辑  收藏  举报