方法notifyDataSetChanged()无法在自定义Listview中工作

时间:2015-02-12 05:04:46

标签: android

我使用适配器扩展BaseAdapter自定义listview。 在getView方法中,我有一个Add按钮,我将侦听器设置为

item.btnAddBasket.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showChooseSize(products.get(pos), pos);
                DialogFragmentCustom.setPosition(pos);
            }
        });

我有两种方法,当我按下按钮时调用。

public void showChooseSize(ProductInfo productInfo, int pos) {
    ArrayList<Stock> stocks = new ArrayList<Stock>();
    stocks.addAll(productInfo.getStock());
    ArrayList<String> colors = new ArrayList<String>();
    ArrayList<String> sizes = new ArrayList<String>();

    if (stocks != null && stocks.size() > 0) {
        for (int i = 0; i < stocks.size() - 1; i++) {
            colors.add(stocks.get(i).getColorName());
            sizes.add(stocks.get(i).getSize());
        }
    }
    if (sizes != null && sizes.size() > 0) {
        HashSet<String> hs_size = new HashSet<String>(); // remove elements
                                                            // duplicate
        hs_size.addAll(sizes);
        sizes.clear();
        sizes.addAll(hs_size);
        if (sizes.size() == 1) {
            size = sizes.get(0);
        } else {
            for (int j = 0; j < sizes.size() - 1; j++) {
                size = size + sizes.get(j) + ", ";
            }
            size = size + sizes.get(sizes.size() - 1);
        }
        a_sizes = null;
        if (size.split(",") != null) {
            a_sizes = size.split(",");
        }
        if (a_sizes == null) {
            a_sizes[0] = "This product do not have size";
        }
        if (a_sizes != null && a_sizes.length > 0) {
            Config.isWishList = true;
            ((Activity) context).setTheme(R.style.ActionSheetStyleIOS7);
            ActionSheetChooseSize.setProductInfo(productInfo);
            ActionSheetChooseSize.createBuilder(context, ((FragmentActivity) context).getSupportFragmentManager()).setOtherButtonTitles(a_sizes)
                    .setCancelableOnTouchOutside(true).setListener(this).show();

            isShowChooseSize = true;
        } else {
            addToBag(productInfo, pos);
        }
    }

按下按钮添加时,将产品添加到Bag中,将其添加到我的适配器

public void addToBag(ProductInfo productInfo, int pos) {
    // Config.productSaleInfos.add(products.get(pos));
    Config.productSaleInfos.add(productInfo);
    Config.numProductSale = Config.productSaleInfos.size();
    Config.numProductWishlist = Config.numProductWishlist - 1 < 0 ? Config.numProductWishlist - 1 : 0;
    //SlideMenuFragment.setNumWish(Config.numProductWishlist + "");
    new DeletionItemWishlist(context).execute(products.get(pos));
    products.remove(pos);
    notifyDataSetChanged();
    float sumaryPrice = 0;
    for (int i = 0; i < products.size(); i++) {
        String price = products.get(i).getSalePrice();
        int index = price.indexOf(",");
        if (index > -1) {
            price = price.replace(",", "");
        }
        float p = Functions.getFloatFromString(price);
        sumaryPrice = sumaryPrice + p;
    }
    basket.setText(Config.numProductSale + "");
    subtotal.setText(Html.fromHtml("<b>Subtotal: </b>" + "<font color=\"#FF7F00\">" + sumaryPrice + " ฿</font>"));
    Functions.showLog(context, "add bag success!");
}

但是notifyDataSetChanged();我的代码没有工作。你能帮帮我吗?

0 个答案:

没有答案