购物车产品从购物车中取出后即可停留

时间:2014-04-16 11:08:51

标签: java android

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

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();

  }
}

1 个答案:

答案 0 :(得分:0)

我建议将数量存储在捆绑中。在重建Activity期间,您可以从Bundle获取数据,并使用getSavedInstanceState在oncreate方法中设置正确的值。