购物车产品即使在取出后仍然存在。 [英] Cart product persists even after removing it.

查看:98
本文介绍了购物车产品即使在取出后仍然存在。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下活动课程。这是一个购物车类,其中添加或删除产品。操作正常,但我有一个奇怪的问题。每当我从购物车中删除所有产品并转到另一个活动时,问题就会开始。当我返回购物车活动时,无论我是否在购物车中添加或删除商品,购物车内总会留有产品。每当我删除该特定项目时,它会显示它被移除并且产品从购物车中消失但是当我返回时,我发现移除的产品保留在购物车中。我无法找出原因。有人请帮帮我。



I have the following activity class. This is a shopping cart class where a product gets added to or removed from. The operation works fine, however i have a strange issue with it. The problem starts whenever i delete all the products from the cart and go to another activity. When i return back to the cart activity, irrespective of whether i add or remove items from the cart there is always a product left inside the cart. Every time I remove that particular item it shows that it is removed and the product disappears from the cart but when i return back, i find that the removed product stays in the cart. I can't find out the cause of this. Somebody please help me with this.

public class Secondscreen extends Activity {


private Context mContext;

int total = 0;
ArrayList<Listitem> arrayList = new ArrayList<Listitem>();
BaseAdapter adapter = null;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen);
    ListView lv = (ListView) findViewById(R.id.listView1);

    // TextView showCartContent = (TextView) findViewById(R.id.showCart);
    final TextView showtotal = (TextView) findViewById(R.id.totalprice);


    final Button thirdBtn = (Button) findViewById(R.id.third);
    Button addmorebtn = (Button) findViewById(R.id.moreitems);


    final Controller aController = (Controller) getApplicationContext();

    int cartSize = aController.getCart().getCartSize();

    // initialize product variables and add item objects to the arraylist


    if (cartSize > 0) {

        for (int i = 0; i < cartSize; i++) {

            String pName = aController.getCart().getProducts(i)
                    .getProductName();
            int pPrice = aController.getCart().getProducts(i)
                    .getProductPrice();
            int pQuantity = aController.getCart().getProducts(i)
                    .getProductQuantity();
            String pDisc = aController.getCart().getProducts(i)
                    .getProductDesc();
            // int pID =aController.getCart().getProducts(i).getId();

            // total calculated
            total = total + (pPrice * pQuantity);

            Listitem item = new Listitem(pName, pPrice, pDisc, pQuantity);
            Log.e("quantity", "" + pQuantity);
            Log.e("Intem's quantity", "" + item.getQuantity());
            arrayList.add(item);
            Log.e("Arraylist item quantity", ""
                    + arrayList.get(i).getQuantity());

        }
        showtotal.setText("" + total);

    }

    adapter = new BaseAdapter() {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        @Override
        public View getView( final int position, View view,
                ViewGroup viewgroup) {
            if (view == null) {
                view = inflater.inflate(R.layout.pattern, null);
            }
            TextView tv = (TextView) view.findViewById(R.id.nameview);
            TextView tv2 = (TextView) view.findViewById(R.id.pdesc);
            TextView tv3 = (TextView) view.findViewById(R.id.priceView);
            TextView tv4 = (TextView) view.findViewById(R.id.quantityView);
            Button btn = (Button) view.findViewById(R.id.patternButton);

            // Remove button actions
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    int tempstore = arrayList.get(position).getPrice();
                    int tempstore2 = arrayList.get(position).getQuantity();

                    //calculation of total
                    total = total - (tempstore * tempstore2);

                    adapter.notifyDataSetChanged();
                    showtotal.setText("" + total);

                    ModelProducts tempProductObject = aController
                            .getProducts(position);
                    //
                    // int pID =aController.updateProduct(position).getId();
                    // Log.e("buttonid", ""+pID);
                    // aController.removeButtonId(position).getId();

                    aController.getCart().removeProducts(tempProductObject);
                    arrayList.remove(tempProductObject);
                    arrayList.remove(position);

                    adapter.notifyDataSetChanged();

                    // FirstScreen obj=new FirstScreen();
                    // obj.onCreate(savedInstanceState);
                    // int cartSize = aController.getCart().getCartSize();


                }

            });

            tv.setText(arrayList.get(position).getName());
            tv2.setText("" + arrayList.get(position).getPrice());
            tv3.setText(arrayList.get(position).getDesc());
            tv4.setText(String.valueOf(arrayList.get(position)
                    .getQuantity()));

            return view;
        }

    };
    adapter.notifyDataSetChanged();

    lv.setAdapter(adapter);


    // int cartSize2 = aController.getCart().getCartSize();
    if (cartSize == 0) {

        Log.e("emptycartmethod", "I am in empty cart sate");

        aController.getCart().getCartlist().clear();
        // Intent intent = new Intent(Secondscreen.this, FirstScreen.class);
        // startActivity(intent);
        finish();
    }

    // Third Button Action

    thirdBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            Intent i = new Intent(getBaseContext(), Thirdscreen.class);
            startActivity(i);

        }
    });

    // AddMoreItem Button Action

    addmorebtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });

}

@Override
protected void onDestroy() {

    super.onDestroy();

  }
}

推荐答案

这篇关于购物车产品即使在取出后仍然存在。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆