基于Android的医院预下单叫号排队系统

效果视频

在这里插入图片描述

提前下单

RecyclerView双列表联动

商品选购界面采用的是两个RecyclerView联动,使用一个第三方封装包BaseQuickAdapter

导入BaseQuickAdapter

一、添加库

maven { url "https://jitpack.io" }

二、添加依赖

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
商品信息实体类

GoodsTitle为左侧商品类型实体类
GoodsContent为右侧商品信息实体类
GoodsTitle、GoodsContent都有一个id字段,为左右联动而设置

public class Goods {
    private List<GoodsTitle> titleList;
    private List<GoodsContent> contentList;
    public static class GoodsTitle{
        private String Title;
        private int id;
        public GoodsTitle(String Title,int id){
            this.Title = Title;
            this.id = id;
        }
        public String getTitle() {
            return Title;
        }
        public void setTitle(String title) {
            Title = title;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }
    public static class GoodsContent{
        private int GoodsIMG;
        private String Content;
        private String Specs;
        private double Price;
        private int Number;
        private int AddIMG;
        private int SubIMG;
        private String Type;
        private int subId;
        public GoodsContent(int GoodsIMG,String Content,String Specs,double Price,int AddIMG,int SubIMG,int Number,int subId){
            this.GoodsIMG = GoodsIMG;
            this.Content = Content;
            this.Specs = Specs;
            this.Price = Price;
            this.AddIMG = AddIMG;
            this.SubIMG = SubIMG;
            this.Number = Number;
            this.subId = subId;
        }

        public int getGoodsIMG() {
            return GoodsIMG;
        }

        public void setGoodsIMG(int goodsIMG) {
            GoodsIMG = goodsIMG;
        }

        public String getContent() {
            return Content;
        }

        public void setContent(String content) {
            Content = content;
        }

        public String getSpecs() {
            return Specs;
        }

        public void setSpecs(String specs) {
            Specs = specs;
        }

        public double getPrice() {
            return Price;
        }

        public void setPrice(double price) {
            Price = price;
        }

        public int getAddIMG() {
            return AddIMG;
        }

        public void setAddIMG(int addIMG) {
            AddIMG = addIMG;
        }

        public int getSubIMG() {
            return SubIMG;
        }

        public void setSubIMG(int subIMG) {
            SubIMG = subIMG;
        }

        public String getType() {
            return Type;
        }

        public void setType(String type) {
            Type = type;
        }

        public int getNumber() {
            return Number;
        }

        public void setNumber(int number) {
            Number = number;
        }

        public int getSubId() {
            return subId;
        }

        public void setSubId(int subId) {
            this.subId = subId;
        }
    }

}
左侧商品类型适配器
public class titleAdapter extends BaseQuickAdapter<Goods.GoodsTitle, BaseViewHolder> {
    public int Position;

    public titleAdapter(int layoutResId, @Nullable List<Goods.GoodsTitle> data) {
        super( layoutResId, data );
    }

    public titleAdapter(@Nullable List<Goods.GoodsTitle> data) {
        super( data );
    }

    public titleAdapter(int layoutResId) {
        super( layoutResId );
    }

    @Override
    protected void convert(BaseViewHolder helper, Goods.GoodsTitle item) {
        helper.setTextColor( R.id.title_name,helper.getLayoutPosition() == Position ? Color.parseColor("#ff0000") : Color.parseColor("#000000") );
        helper.setText( R.id.title_name,item.getTitle() );

    }
    public void setSelection(int Position) {
        this.Position = Position;
        notifyDataSetChanged();
    }
右侧商品信息适配器
public class contentAdapter extends BaseQuickAdapter<Goods.GoodsContent, BaseViewHolder> {


    public contentAdapter(int layoutResId, @Nullable List<Goods.GoodsContent> data) {
        super( layoutResId, data );
    }

    public contentAdapter(@Nullable List<Goods.GoodsContent> data) {
        super( data );
    }

    public contentAdapter(int layoutResId) {
        super( layoutResId );
    }

    @Override
    protected void convert(BaseViewHolder helper, Goods.GoodsContent item) {
        helper.setText( R.id.medicineName,item.getContent() );
        helper.setText( R.id.medicineSpecs,item.getSpecs() );
        helper.setText( R.id.medicinePrice,item.getPrice()+"" );
        helper.setText( R.id.Number,item.getNumber()+"" );
        helper.setImageResource( R.id.medicineIMG,item.getGoodsIMG() );
        helper.setImageResource( R.id.Add,item.getAddIMG() );
        helper.setImageResource( R.id.Sub,item.getSubIMG() );
        helper.addOnClickListener( R.id.Add );
        helper.addOnClickListener( R.id.Sub );
    }
}
初始化左右两个RecyclerView
private void InitRecyclerView(){
        titleManager = new LinearLayoutManager( getApplicationContext() );
        mGoodsTitle.setLayoutManager( titleManager );
        titleAdapter = new titleAdapter( R.layout.title_item,titleList );
        mGoodsTitle.setAdapter( titleAdapter );



        contentManager = new LinearLayoutManager( getApplicationContext() );
        mGoodsContent.setLayoutManager( contentManager );
        contentAdapter = new contentAdapter( R.layout.content_item,contentList );
        mGoodsContent.setAdapter( contentAdapter );

    }
传入数据源
private void InitTitle(){
        for (int i = 0; i <resources.listTitleName.length; i++) {
            goodsTitle = new Goods.GoodsTitle( resources.listTitleName[i],i );
            titleList.add( goodsTitle );
        }
    }
    private void InitContent(){
        for (int i = 0; i <resources.medicineContent.length ; i++) {
            /*感冒系列*/
            if ( i < 10){
                goodsContent = new Goods.GoodsContent( resources.medicineImg[i],resources.medicineContent[i],resources.medicineSpec[i],resources.medicinePrice[i],R.drawable.jiahao,R.drawable.jianhao,0,0 );
            }else if (i >= 10 && i < 20){
                /*家庭常用系列*/
                goodsContent = new Goods.GoodsContent( resources.medicineImg[i],resources.medicineContent[i],resources.medicineSpec[i],resources.medicinePrice[i],R.drawable.jiahao,R.drawable.jianhao,0,1);
            }else if (i >=20 && i  < 30){
                /*幸福生活系列*/
                goodsContent = new Goods.GoodsContent( resources.medicineImg[i],resources.medicineContent[i],resources.medicineSpec[i],resources.medicinePrice[i],R.drawable.jiahao,R.drawable.jianhao,0,2);
            }else if (i >= 30 && i < 35){
                /*妇科系列*/
                goodsContent = new Goods.GoodsContent( resources.medicineImg[i],resources.medicineContent[i],resources.medicineSpec[i],resources.medicinePrice[i],R.drawable.jiahao,R.drawable.jianhao,0,3);
            }else if (i >= 35 && i < 40){
                /*男科系列*/
                goodsContent = new Goods.GoodsContent( resources.medicineImg[i],resources.medicineContent[i],resources.medicineSpec[i],resources.medicinePrice[i],R.drawable.jiahao,R.drawable.jianhao,0,4);
            }
            contentList.add( goodsContent );
        }
    }
列表联动

在传入数据时,为左侧每一个类型传入不同的id,右侧商品信息根据数目传入不同的id,在右侧联动左侧时,对左侧id和右侧id进行匹配,然后移动左侧标题栏,实现列表联动

左侧联动右侧
  titleAdapter.setOnItemClickListener( new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                titleAdapter.setSelection( position );
                titleAdapter.notifyDataSetChanged();
                contentManager.scrollToPositionWithOffset( resources.listTitleName.length,0 );
               // mGoodsTitle.smoothScrollToPosition( resources.listTitleName.length );
                //contentAdapter.notifyDataSetChanged();
            }
        } );
右侧联动左侧
  mGoodsContent.addOnScrollListener( new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled( recyclerView, dx, dy );
                int firstPosition = contentManager.findFirstVisibleItemPosition();
                goodsContent = contentList.get(firstPosition);
                int subId = goodsContent.getSubId();
                int pos = 0;
                for (int i = 0; i < titleList.size(); i++) {
                    int id = titleList.get(i).getId();
                    if ((subId == id)) {
                        pos = i;
                    }
                }
                titleAdapter.setSelection(pos);
                //mGoodsTitle.scrollToPosition( pos );
                titleAdapter.notifyDataSetChanged();

            }
        } );

子项添加点击

在适配器中添加如下定义,传入需要添加点击事件的子项ID

helper.addOnClickListener( R.id.Add );
 helper.addOnClickListener( R.id.Sub );

源码如下

 /**
     * add childView id
     *
     * @param viewId add the child view id   can support childview click
     * @return if you use adapter bind listener
     * @link {(adapter.setOnItemChildClickListener(listener))}
     * <p>
     * or if you can use  recyclerView.addOnItemTouch(listerer)  wo also support this menthod
     */
    @SuppressWarnings("unchecked")
    public BaseViewHolder addOnClickListener(@IdRes final int viewId) {
        childClickViewIds.add(viewId);
        final View view = getView(viewId);
        if (view != null) {
            if (!view.isClickable()) {
                view.setClickable(true);
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (adapter.getOnItemChildClickListener() != null) {
                        adapter.getOnItemChildClickListener().onItemChildClick(adapter, v, getClickPosition());
                    }
                }
            });
        }

        return this;
    }

然后再实现setOnItemChildClickListener方法即可
点击加号按钮时,商品数量增加,价格也增加,通过修改实体类字段的内容,然后通过如下语句刷新,减号按钮同理

contentAdapter.notifyDataSetChanged();
 contentAdapter.setOnItemChildClickListener( new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                double  Price = contentList.get( position ).getPrice();
                switch (view.getId()){
                    case R.id.Add:
                        int numberAdd = contentList.get( position ).getNumber();
                        numberAdd++;
                        Monetary(Price);
                        contentList.get( position ).setNumber( numberAdd );
                        contentAdapter.notifyDataSetChanged();
                        //Toast.makeText( FunctionActivity.this,"Add"+Price,Toast.LENGTH_SHORT ).show();
                        break;
                    case R.id.Sub:
                        int numberSub = contentList.get( position ).getNumber();
                        if (numberSub > 0 ){
                            numberSub--;
                            contentList.get( position ).setNumber( numberSub );
                            LessMoney(Price);
                        }else if (numberSub < 0){
                            contentList.get( position ).setNumber( 0 );
                        }
                        contentAdapter.notifyDataSetChanged();
                        //Toast.makeText( FunctionActivity.this,"Sub"+Price,Toast.LENGTH_SHORT ).show();
                        break;
                }
            }
        } );

选中的商品插入与显示

效果图
将选中的商品,存入数据库
创建数据库实体类
public class GoodsList {
        private int Img;
        private String Name;
        private double Price;
        private int Number;
        private int Sub;
        private int Add;
        public GoodsList(int Img,String Name,double Price,int Number,int Sub,int Add){
            this.Img = Img;
            this.Name = Name;
            this.Price = Price;
            this.Number = Number;
            this.Sub = Sub;
            this.Add = Add;
        }
        public GoodsList(int Img,String Name,double Price,int Number){
            this.Img = Img;
            this.Name = Name;
            this.Price = Price;
            this.Number = Number;
        }

        public int getImg() {
            return Img;
        }

        public void setImg(int img) {
            Img = img;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }

        public double getPrice() {
            return Price;
        }

        public void setPrice(double price) {
            Price = price;
        }

        public int getNumber() {
            return Number;
        }

        public void setNumber(int number) {
            Number = number;
        }

        public int getSub() {
            return Sub;
        }

        public void setSub(int sub) {
            Sub = sub;
        }

        public int getAdd() {
            return Add;
        }

        public void setAdd(int add) {
            Add = add;
        }

}

建表
public class Helper extends SQLiteOpenHelper {
    /**数据库名称*/
    public static final String DataBase = "HospitalSystem.db";
    public static final SQLiteDatabase.CursorFactory factory = null;
    public static int version = 5;
    /**表名1*/
    public static final String TableName = "GoodsList";
    /**行名*/
    public static final String Row_ID = "ID";
    public static final String Row_GoodsImg = "GoodsImg";
    public static final String Row_GoodsName = "GoodsName";
    public static final String Row_GoodsPrice = "GoodsPrice";
    public static final String Row_GoodsNumber = "GoodsNumber";
    public static final String Row_GoodsSub = "GoodsSub";
    public static final String Row_GoodsAdd= "GoodsAdd";



    public Helper(@Nullable Context context) {
        super( context, DataBase, factory, version );
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TableName + "("+Row_ID+" INTEGER primary key AUTOINCREMENT,"+Row_GoodsImg+" INTEGER, "+Row_GoodsName+" varchar(20),"+Row_GoodsPrice+" REAL,"+Row_GoodsNumber+" INTEGER,"+Row_GoodsSub+" NUMERIC,"+Row_GoodsAdd+" NUMERIC);";
        //String sql_Information = "create table " + InformationTable + "("+Row_InformationAddress+" varchar(20) primary key, "+Row_InformationParkType+" varchar(20),"+Row_InformationShareMode+" varchar(20),"+Row_InformationLastName+" varchar(20),"+Row_InformationSex+" varchar(20),"+Row_InformationPhone+" varchar(20));";
        db.execSQL( sql );
        //db.execSQL( sql_Information );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion){
            case 4:
                String sql = "create table " + TableName + "("+Row_ID+" INTEGER primary key AUTOINCREMENT,"+Row_GoodsImg+" INTEGER, "+Row_GoodsName+" varchar(20),"+Row_GoodsPrice+" REAL,"+Row_GoodsNumber+" INTEGER,"+Row_GoodsSub+" NUMERIC,"+Row_GoodsAdd+" NUMERIC);";
                db.execSQL( sql );
            case 5:
                String sql5 = "create table " + TableName + "("+Row_ID+" INTEGER primary key AUTOINCREMENT,"+Row_GoodsImg+" INTEGER, "+Row_GoodsName+" varchar(20),"+Row_GoodsPrice+" REAL,"+Row_GoodsNumber+" INTEGER,"+Row_GoodsSub+" NUMERIC,"+Row_GoodsAdd+" NUMERIC);";
                db.execSQL( sql5 );
        }
        //db.execSQL("drop table if exists "+TableName);
        //db.execSQL("drop table if exists "+InformationTable);
        //onCreate(db);
    }
}
建立Dao类,封装CRUD
public class Dao {
    public static final String TAG = "DataBase";
    private Helper helper ;
    private SQLiteDatabase DB;
    public Dao(Context context)
    {
        helper = new Helper(context);
    }
    public void Insert(GoodsList goodsList){
        DB = helper.getReadableDatabase();
        if (DB.isOpen())
        {
            ContentValues values = new ContentValues();
            values.put( Helper.Row_GoodsImg,goodsList.getImg());
            values.put( Helper.Row_GoodsName,goodsList.getName());
            values.put( Helper.Row_GoodsNumber,goodsList.getNumber());
            values.put( Helper.Row_GoodsPrice,goodsList.getPrice());
            values.put( Helper.Row_GoodsSub,goodsList.getSub() );
            values.put( Helper.Row_GoodsAdd,goodsList.getAdd() );
            long RowId = DB.replace(Helper.TableName,null,values);
            if(RowId == -1)
                Log.i(TAG, "数据插入失败!");
            else
                Log.i(TAG, "数据插入成功!"+RowId);
        }
    }
    //根据序号删除数据
    //当前id为暂不为序号
    public void Delete(){
        DB = helper.getReadableDatabase();
        if (DB.isOpen()){
            String whereClause = null;
            String[] whereArgs = null;
            int count = DB.delete(Helper.TableName, whereClause, whereArgs);
            if (count > 0)
                Log.i(TAG, "删除了: " + count + "行");
            else
                Log.i(Helper.TableName, "数据未删除!");
            DB.close(); // 数据库关闭
        }
    }
//    public void Update(Account account){
//        DB = helper.getWritableDatabase();
//        if(DB.isOpen()) {   // 如果数据库打开, 执行更新的操作
//            ContentValues values = new ContentValues();
//            //values.put( Helper.Row_Account,account.getUserID());
//            values.put( Helper.Row_UserName,account.getUserName());
//            //values.put( Helper.Row_PassWord,account.getPassWord());
//            values.put( Helper.Row_Sex,account.getSex());
//            values.put( Helper.Row_PhoneNumber,account.getPhoneNumber());
//            int count  = DB.update(Helper.TableName, values, "Account = ?", new String[]{account.getUserID() + ""});
//            if (count > 0)
//                Log.i(TAG, "修改了: " + count + "行");
//            else
//                Log.i(Helper.TableName, "数据未删除!");
//            DB.close(); // 数据库关闭
//        }
//    }
//    public String Query(String Account){
//        DB = helper.getReadableDatabase();
//        //selection查询子句的条件,可以使用主键查询
//        String[] columns = {Helper.Row_Account,Helper.Row_PassWord};  // 需要的列
//        String selection = "Account = ?";
//        String[] selectionArgs = {Account + ""};
//        Cursor cursor = DB.query(Helper.TableName,columns,selection,selectionArgs,null,null,null);
//        if (cursor.moveToFirst())
//        {
//                //String _id = cursor.getString(0);
//                String Content = cursor.getString(cursor.getColumnIndex( Helper.Row_PassWord ));
//                return Content;
//        }
//        DB.close();
//        return null;
//    }
    public List<GoodsList> QueryAll() {
        DB = helper.getReadableDatabase();  // 获得一个只读的数据库对象
        if(DB.isOpen()) {
            String[] columns = {Helper.Row_GoodsImg, Helper.Row_GoodsName,Helper.Row_GoodsNumber,Helper.Row_GoodsPrice};  // 需要的列
            String selection = null;    // 选择条件, 给null查询所有
            String[] selectionArgs = null;  // 选择条件的参数, 会把选择条件中的? 替换成数据中的值
            String groupBy = null;  // 分组语句  group by name
            String having = null;   // 过滤语句
            String orderBy = null;  // 排序
            Cursor cursor = DB.query(Helper.TableName, columns, selection, selectionArgs, groupBy, having, orderBy);
            int mImg;
            String mName;
            double mPrice;
            int mNumber;
            int Add;
            int Sub;
            if(cursor != null && cursor.getCount() > 0) {
                List<GoodsList> goodsLists = new ArrayList<GoodsList>();
                while(cursor.moveToNext()) {    // 向下移一位, 知道最后一位, 不可以往下移动了, 停止.
                    mImg = cursor.getInt(cursor.getColumnIndex( "GoodsImg" ));
                    mName = cursor.getString(cursor.getColumnIndex( "GoodsName" ));
                    mPrice = cursor.getDouble(cursor.getColumnIndex( "GoodsPrice"));
                    mNumber = cursor.getInt(cursor.getColumnIndex( "GoodsNumber"));
                    Add =  R.drawable.jiahao;
                    Sub = R.drawable.jianhao;
                    goodsLists.add(new GoodsList(mImg,mName,mPrice,mNumber,Sub,Add));
                }
                DB.close();
                return goodsLists;
            }
            DB.close();
        }
        return null;
    }
}
插入选中的商品信息至数据库

通过判断选中商品数量,将大于0的商品信息插入数据库

private void GotoSettlement(){
        for (int i = 0; i <contentList.size() ; i++) {
            int num = contentList.get( i ).getNumber();
            if (num > 0){
                /*插入数据为null*/
                //Toast.makeText( context,""+num,Toast.LENGTH_SHORT ).show();
                goodsList = new GoodsList( contentList.get( i ).getGoodsIMG(),contentList.get( i ).getContent(),contentList.get( i ).getPrice(),contentList.get( i ).getNumber(),R.drawable.jianhao,R.drawable.jiahao );
                dao.Insert( goodsList );
            }
        }
    }
取出选中的商品信息从数据库
private void InitData(){
        goodsLists = dao.QueryAll();
        for (int i = 0; i <goodsLists.size() ; i++) {
            goodsList = new GoodsList( goodsLists.get( i ).getImg(),goodsLists.get( i ).getName(),goodsLists.get( i ).getPrice(),goodsLists.get( i ).getNumber(),goodsLists.get( i ).getSub(),goodsLists.get( i ).getAdd() );
            dataList.add( goodsList );
        }
    }
显示选中的商品

同样使用RecyclerView展示,实体类、适配器等省略

效果图
子项数量为0删除子项

在适配器中添加如下方法

public void subPrice(List<GoodsList> item,double price,int position,int number){
        number--;
        if (number > 0){
            totalPrice -= price;
             item.get( position ).setNumber( number );
        }else {
           item.remove( position );
        }
        notifyDataSetChanged();
    }
子项清空

添加如下方法

public void removeAllItem(List<GoodsList> item){
            item.removeAll( item );
            notifyDataSetChanged();
    }
商品总数统计
public int TotalNumber(List<GoodsList> item){
        int num = 0;
        for (int i = 0; i <item.size() ; i++) {
            num += item.get( i ).getNumber();
        }
        return num;
    }
商品总价统计
public double TotalPrice(List<GoodsList> item){
        double totalPrice = 0;
        for (int i = 0; i < item.size(); i++) {
            int num = item.get( i ).getNumber();
            double price = item.get( i ).getPrice();
            totalPrice += num * price;
        }
        return totalPrice;
    }

排队系统

参考另外一篇博客排队系统

优雅谢幕

killProcess

杀死所有Activity,添加如下类

public class killProcess extends Application {
    private List<Activity> activityList = new ArrayList<>(  );
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }
    public void finishAll() {
        for (Activity activity : activityList) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}

应用

添加Activity

在每一个Activity的onCreate方法使用如下语句

 killProcess = new killProcess();
 killProcess.addActivity( GoodsListActivity.this );
杀死所有Activity
killProcess.finishAll();

尾言

花花世界,其心不恒,淤泥不染尚不可为之,唯独善其身,远离喧嚣。

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FranzLiszt1847

嘟嘟嘟嘟嘟

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值