带有数量增量和减量按钮的列表视图

时间:2016-08-16 08:11:50

标签: android xml

在listview中,我有一个增量和减量按钮,用于更改数量。增加和减少的操作工作正常。但数量将在其他活动中显示在数组中。请帮助我,我是Android开发的新手。 first activitysecond activity点按

MainActivity

public class CatalogActivity extends Activity implements CustomButtonListener{

    private List<Product> mProductList;
    static  int quantity2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.catalog);

        // Obtain a reference to the product catalog
        mProductList = ShoppingCartHelper.getCatalog(getResources());

        // Make sure to clear the selections
        for(int i=0; i<mProductList.size(); i++) {
            mProductList.get(i).selected = false;
        }


        // Create the list
        ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);

        listViewCatalog.setAdapter(new ProductAdapter(mProductList, getLayoutInflater(), false));



        Button viewShoppingCart = (Button) findViewById(R.id.ButtonViewCart);

        ProductAdapter.setCustomButtonListener(this);
        viewShoppingCart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class);
                startActivity(viewShoppingCartIntent);
            }
        });

    }

    @Override
    public void onButtonClickListener(int position, TextView editText, int value) {

        quantity2 = Integer.parseInt(editText.getText().toString());
        quantity2 = quantity2+1*value;
        if(quantity2<0)
            quantity2=0;
        editText.setText(quantity2+"");


    }
}

产品适配器

public class ProductAdapter extends BaseAdapter {
    private List<Product> mProductList;
    private LayoutInflater mInflater;
    private boolean mShowQuantity;
    public static int quantity = 0;
    public Context context;
    public static ArrayList<Integer> quantity1 = new ArrayList<Integer>();
    static CustomButtonListener customButtonListener;
    public static int quant;
    public ProductAdapter(List<Product> list, LayoutInflater inflater, boolean showQuantity) {
        mProductList = list;
        mInflater = inflater;
        mShowQuantity = showQuantity;
        for (int i = 0; i < list.size(); i++) {
            quantity1.add(0);

        }
    }
    public static void setCustomButtonListener(CustomButtonListener customButtonListner) {
        customButtonListener = customButtonListner;
    }
    @Override
    public int getCount() {
        return mProductList.size();
    }
    @Override
    public Product getItem(int position) {
        return mProductList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewItem item;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item, null);
            item = new ViewItem();
            item.productImageView = (ImageView) convertView
            .findViewById(R.id.ImageViewItem);
            item.productTitle = (TextView) convertView
            .findViewById(R.id.TextViewItem);
            item.productQuantity = (TextView) convertView
            .findViewById(R.id.textViewQuantity);
            item.productCost = (TextView) convertView
            .findViewById(R.id.itemcost);
            item.productbtn = (Button) convertView.findViewById(R.id.btn_plus);
            item.textquanty = (TextView) convertView.findViewById(R.id.num);
            item.productbtnminus = (Button) convertView.findViewById(R.id.btn_minus);
            item.Layout = (View) convertView.findViewById(R.id.add);
            getItem(position).setSelected(false);
            convertView.setTag(item);
        } else {
            item = (ViewItem) convertView.getTag();
        }
        final Product product = getItem(position);
        try {
            item.textquanty.setText(quantity1.get(position) + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        item.productbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if (customButtonListener != null) {
                    customButtonListener.onButtonClickListener(position, item.textquanty, 1);
                    quantity1.set(position, quantity1.get(position) + 1);
                    ShoppingCartHelper.setQuantity(curProduct, quantity1, position);
                }
            }
        });
        item.productbtnminus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (customButtonListener != null) {
                    customButtonListener.onButtonClickListener(position, item.textquanty, -1);
                    if (quantity1.get(position) > 0)
                        quantity1.set(position, quantity1.get(position) - 1);
                    ShoppingCartHelper.setQuantity(curProduct, quantity1, position);
                }
            }
        });
        item.productImageView.setImageDrawable(product.productImage);
        item.productTitle.setText(product.title);
        item.productCost.setText("" + product.price);
        if (mShowQuantity) {
            item.productQuantity.setText("Quantity: " + ShoppingCartHelper.getProductQuantity(product, position));
            item.Layout.setVisibility(View.GONE);
        } else {
            item.productQuantity.setVisibility(View.GONE);
        }
        return convertView;
    }
    public static Integer getProductQuantity(Product product, int position) {
        ShoppingCartEntry curEntry = ShoppingCartHelper.cartMap.get(product);
        if (curEntry != null)
            quant=Integer.parseInt((quantity1.get(position)+""));

        return quant;
    }
    private class ViewItem {
        ImageView productImageView;
        TextView productTitle;
        TextView productQuantity;
        TextView productCost;
        public Button productbtn;
        public Button productbtnminus;
        View Layout;
        public TextView textquanty;
    }
}

产品

public class Product {
    public String title;
    public Drawable productImage;
    public String description;
    public double price;
    private int quantity;

    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Product(String title, Drawable productImage, String description,
        double price) {
        this.title = title;
        this.productImage = productImage;
        this.description = description;
        this.price = price;
    }
}

ShoppingCartEntry

public class ShoppingCartEntry {
    private Product mProduct;
    private ArrayList<Integer> mQuantity;
    public ShoppingCartEntry(Product product, ArrayList<Integer> quantity1) {
        mProduct = product;
        mQuantity = quantity1;
    }
    public Product getProduct() {
        return mProduct;
    }
    public ArrayList<Integer> getQuantity(int position) {
        return mQuantity;
    }
    public void setQuantity(ArrayList<Integer> quantity1) {
        mQuantity = quantity1;
    }
}

ShoppingCartHelper

public class ShoppingCartHelper {
    public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
    protected static List<Product> catalog;
    public static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();
    public static List<Product> getCatalog(Resources res){
        if(catalog == null) {
            catalog = new Vector<Product>();
            catalog.add(new Product("Chiken Curry", res .getDrawable(R.drawable.deadoralive),"Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));
            catalog.add(new Product("Mutton Curry", res .getDrawable(R.drawable.switchbook), "Switch by Chip Heath and Dan Heath", 24.99));
            catalog.add(new Product("Aloo Curry", res.getDrawable(R.drawable.watchmen), "Watchmen by Alan Moore and Dave Gibbons", 14.99));
            catalog.add(new Product("ChikenBiryani", res .getDrawable(R.drawable.deadoralive),"Dead or Alive by Tom Clancy with Grant Blackwood", 25.99));
            catalog.add(new Product("MuttonBiryani", res .getDrawable(R.drawable.switchbook), "Switch by Chip Heath and Dan Heath", 21.99));
            catalog.add(new Product("Aloo Curry", res.getDrawable(R.drawable.watchmen), "Watchmen by Alan Moore and Dave Gibbons", 12.99));
            catalog.add(new Product("Chiken Curry", res .getDrawable(R.drawable.deadoralive),"Dead or Alive by Tom Clancy with Grant Blackwood", 30.99));
            catalog.add(new Product("Mutton Curry", res .getDrawable(R.drawable.switchbook), "Switch by Chip Heath and Dan Heath", 28.99));
            catalog.add(new Product("Aloo Curry", res.getDrawable(R.drawable.watchmen), "Watchmen by Alan Moore and Dave Gibbons", 18.99));
        }
        return catalog;
    }
    public static void setQuantity(Product product, ArrayList<Integer> quantity1, int position) {
        // Get the current cart entry
        ShoppingCartEntry curEntry = cartMap.get(product);
        // If the quantity is zero or less, remove the products
        if(ProductAdapter.quantity1.get(position) <= 0) {
            if(curEntry != null)
                removeProduct(product);
            return;
        }
        // If a current cart entry doesn't exist, create one
        if(curEntry == null) {
            curEntry = new ShoppingCartEntry(product, quantity1);
            cartMap.put(product, curEntry);
            return;
        }
        // Update the quantity
        curEntry.setQuantity(quantity1);
    }
    public static ArrayList<Integer> getProductQuantity(Product product, int position) {
        // Get the current cart entry
        ShoppingCartEntry curEntry = cartMap.get(product);
        if(curEntry != null)
            return curEntry.getQuantity(position);
        return null;
    }
    public static void removeProduct(Product product) {
        cartMap.remove(product);
    }
    public static List<Product> getCartList() {
        List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
        for(Product p : cartMap.keySet()) {
            cartList.add(p);
        }
        return cartList;
    }
}

0 个答案:

没有答案