安卓应用之简易购物车实现

基本要求

1、分三个界面,商品界面、购物车界面和商品详情界面

2、使用adapter填充数据

3、点击购买后购物车进入购物车可显示相应商品

4、能够在本地保存商品,即当下一次打开app时购物车能够显示购买的商品

5、清空购物车,并在清空时显示总价格

�开发环境

1)JDK(Java Development Kit)JDK是Java语言的软件开发工具包,主要用于移动设备、嵌入设备上的java应用程序。

2)SDK(software development kit) SDK是软件开发工具包。 被软件开发工程师用于为特定的软件包、软件框架、硬件平台、操作系统等建立应用软件的开发工具的集合

作者:zwb

界面设计

主界面



    
        
            
            
        
        
        

        
        
    

安卓应用之简易购物车实现_第1张图片
Paste_Image.png

结算界面




    

    

安卓应用之简易购物车实现_第2张图片
Paste_Image.png

展示商品详细信息的界面




    
    


安卓应用之简易购物车实现_第3张图片
Paste_Image.png

为主界面的listview设置一个子布局




    
    
        
        
    

    

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private List shoppingItems = new ArrayList<>();
    private ShoppingItemDataopenHelper shoppingItemDataopenHelper;
    private SQLiteDatabase sqLiteDatabase;
    private ListView listView;
    private ImageView btnbuy;
    private boolean isFirstCheck;
    private boolean isListChanged;
    private LocalBroadcastManager localBroadcastManager;
    private MyReceiver myReceiver;

    class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!isListChanged){
                isListChanged=true;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(myReceiver);
    }
    private void initView(){
        isFirstCheck=true;
        isListChanged=false;
        localBroadcastManager=LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter=new IntentFilter("com.example.shoppingcar.MAINACTIVITY");
        myReceiver=new MyReceiver();
        localBroadcastManager.registerReceiver(myReceiver,intentFilter);
        shoppingItemDataopenHelper=new ShoppingItemDataopenHelper(MainActivity.this,"shoppingitems.db",null,1);
        sqLiteDatabase=shoppingItemDataopenHelper.getWritableDatabase();
        listView=(ListView)findViewById(R.id.listview);
        btnbuy=(ImageView) findViewById(R.id.btnbuy);
        initshoppingitems();
        ShoppingItemAdapter shoppingItemAdapter=new ShoppingItemAdapter(MainActivity.this,R.layout.shoppingitemleyout,shoppingItems);
        listView.setAdapter(shoppingItemAdapter);
        shoppingItemAdapter.notifyDataSetChanged();
        btnbuy.setOnClickListener(new View.OnClickListener() {
            double priceaddAll;
            String itemshad;
            @Override
            public void onClick(View view) {
                Cursor cursor=sqLiteDatabase.query("Shoppinglist",null,null,null,null,null,null);
                if (isFirstCheck&&!isListChanged){
                    if (cursor!=null) {
                        cursor.moveToFirst();
                        priceaddAll = cursor.getDouble(cursor.getColumnIndex("priceOfItems"));
                        itemshad = cursor.getString(cursor.getColumnIndex("namesOfItems"));
                    }else {
                        priceaddAll=0.00;
                        itemshad="您没有购买任何商品";
                    }
                    isFirstCheck=false;
                    Intent intent = new Intent(MainActivity.this, jiesuanActivity.class);
                    intent.putExtra("itemshad", itemshad);
                    intent.putExtra("priceaddAll", priceaddAll);
                    startActivity(intent);
                    return;
                }
                if (isFirstCheck&&isListChanged){
                    priceaddAll=0.00;
                    itemshad="您购买的商品有:";
                    for (ShoppingItem item:shoppingItems){
                        if (item.ischosen()){
                            priceaddAll=priceaddAll+item.getPrice();
                            itemshad=itemshad+"\n"+item.getItemname();
                        }
                    }
                    if (priceaddAll==0.00){
                        itemshad="您没有购买任何商品";
                    }
                    if (cursor!=null){
                        sqLiteDatabase.delete("Shoppinglist",null,null);
                    }
                    ContentValues contentValues=new ContentValues();
                    contentValues.put("priceOfItems",priceaddAll);
                    contentValues.put("namesOfItems",itemshad);
                    sqLiteDatabase.insert("Shoppinglist",null,contentValues);
                    isListChanged=true;
                    isFirstCheck=false;
                    Intent intent=new Intent(MainActivity.this,jiesuanActivity.class);
                    intent.putExtra("itemshad",itemshad);
                    intent.putExtra("priceaddAll",priceaddAll);
                    startActivity(intent);
                    return;
                }
                priceaddAll=0.00;
                itemshad="您购买的商品有:";
                for (ShoppingItem item:shoppingItems){
                    if (item.ischosen()){
                        priceaddAll=priceaddAll+item.getPrice();
                        itemshad=itemshad+"\n"+item.getItemname();
                    }
                }
                if (priceaddAll==0.00){
                    itemshad="您没有购买任何商品";
                }
                if (cursor!=null){
                    sqLiteDatabase.delete("Shoppinglist",null,null);
                }
                ContentValues contentValues=new ContentValues();
                contentValues.put("priceOfItems",priceaddAll);
                contentValues.put("namesOfItems",itemshad);
                sqLiteDatabase.insert("Shoppinglist",null,contentValues);
                Intent intent=new Intent(MainActivity.this,jiesuanActivity.class);
                intent.putExtra("itemshad",itemshad);
                intent.putExtra("priceaddAll",priceaddAll);
                startActivity(intent);
            }
        });
    }

    public void initshoppingitems(){
        ShoppingItem cake=new ShoppingItem("蛋糕",R.drawable.present,5.00,"单价:5.00元\n出产地:广州\n保质期:180天\n生产日期:2017年1月1日");
        ShoppingItem love=new ShoppingItem("爱心",R.drawable.love,10.00,"单价:10.00元");
        ShoppingItem mouse=new ShoppingItem("鼠标",R.drawable.mouse,200.00,"出产地:深圳\n单价:200元");
        ShoppingItem musiccd=new ShoppingItem("唱片",R.drawable.musiccd,200.00,"单价:30.00元");
        ShoppingItem present=new ShoppingItem("礼物",R.drawable.cake,150.00,"单价:150.00元");
        ShoppingItem stamp=new ShoppingItem("邮票",R.drawable.stamp,0.10,"单价:0.10元");
        shoppingItems.add(cake);
        shoppingItems.add(love);
        shoppingItems.add(mouse);
        shoppingItems.add(musiccd);
        shoppingItems.add(present);
        shoppingItems.add(stamp);
    }
}

我们看到要求的第四点:能够在本地保存商品,即当下一次打开app时购物车能够显示购买的商品。
每一次打开app,将isFirstChecked(变量用于判断是否第一次查看已购买的商品)初始化为true,将isListChanged(判断第一次查看前是否对购物清单进行了修改)初始化为false。
打开app并点击结算旁边的imagebutton时,可能出现以下情况:
1、第一次按这个按钮,不点击checkbox复选框对已买的商品进行任何修改,直接按。这时判断查询数据库的游标cursor是否为空,如果为空(之前没打开过app加过东西),输出总价为0,没有买任何商品;cursor不为空(之前打开过app加过东西),移到第一行读出数据库中储存的商品总价、购物清单。
2、直接修改(按checkbox)然后第一次按。遍历shoopingitems清单,读出checkbox勾选了的商品,进行总价的计算、已购买商品名称的统计(加到一个字符串里),更新数据库内容,然后跳转到另一个Activity。
以上两种操作结束后都将isFirstChecked值改为false,于是就不会进行前两种判断了。
然后其他情况(不是第一次点的话)就看cursor查出来数据库内容是否为空,空的话就直接插入内容。非空就直接删掉数据库内容,插入最新的商品总价和已购买清单。

适配器

public class ShoppingItemAdapter extends ArrayAdapter {
    private List myshoppingitems;
    private int ResourceId;
    private LocalBroadcastManager localBroadcastManager;
    public ShoppingItemAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List objects) {
        super(context, resource, objects);
        myshoppingitems=objects;
        ResourceId=resource;
        localBroadcastManager=LocalBroadcastManager.getInstance(context);
    }
    class ViewHolder{
        ImageView itemimage;
        TextView itemname;
        CheckBox checkBox;
        Button btnitemdetail;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final ShoppingItem shoppingItem=(ShoppingItem)getItem(position);
        final View view;
        final ViewHolder viewHolder;
        if (convertView==null){
            view= LayoutInflater.from(getContext()).inflate(ResourceId,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.itemimage=(ImageView)view.findViewById(R.id.imageview);
            viewHolder.itemname=(TextView)view.findViewById(R.id.textview);
            viewHolder.checkBox=(CheckBox)view.findViewById(R.id.checkbox);
            viewHolder.btnitemdetail=(Button)view.findViewById(R.id.btndetail);
            view.setTag(viewHolder);
        }else {
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.itemimage.setImageResource(shoppingItem.getImageId());
        viewHolder.itemname.setText(shoppingItem.getItemname());
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                shoppingItem.setIschosen(b);
                Intent intent=new Intent("com.example.shoppingcar.MAINACTIVITY");
                localBroadcastManager.sendBroadcast(intent);
            }
        });
        viewHolder.btnitemdetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),DetailActivity.class);
                intent.putExtra("iteminformation",shoppingItem.getInformation());
                intent.putExtra("itemimage",shoppingItem.getImageId());
                getContext().startActivity(intent);
            }
        });
        return view;
    }
}

SQliteOpenHelper的子类用于进行数据库的增删改查操作

public class ShoppingItemDataopenHelper extends SQLiteOpenHelper {
    private Context mContext;
    private static final String CREATE_DATABASE="create table Shoppinglist("
            +"id integer primary key autoincrement,"
            +"namesOfItems text,"
            +"priceOfItems real)";
    public ShoppingItemDataopenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_DATABASE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

你可能感兴趣的:(安卓应用之简易购物车实现)