如何在listView - android中设置一个按钮的可见性,点击另一个按钮

时间:2015-11-28 08:15:12

标签: android

我想点击btnProductDetailAddToCart将btnProductDetailMinus的可见性设置为true。这是我的代码。

public class ProductVariantAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<ProductVariant> productVariants = new ArrayList<>();
    /*private view holder class*/
    private class ViewHolder {
        TextView productVariantName;
        TextView productVariantMrp;
        TextView productVariantSellPrice;
        Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
    }

    public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
        this.context = context;
        this.productVariants = productVariants;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_product_variant, null);
            holder = new ViewHolder();
            holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
            holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
            holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
            holder.btnProductDetailAddToCart = (Button)  convertView.findViewById(R.id.btnProductDetailAddToCart);
            holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
            holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
            holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ProductVariant productVariant = (ProductVariant) getItem(position);
        if (productVariant != null) {
            holder.productVariantName.setText(productVariant.getVariant().getVariantName());
            holder.productVariantMrp.setText(productVariant.getMrp().toString());
            holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
            holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utility.displayToast("wonderful");
                    holder.btnProductDetailMinus.setVisibility(View.VISIBLE);

                }

            });

        } else {
            Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return productVariants.size();
    }

    @Override
    public Object getItem(int position) {
        return productVariants.get(position);
    }

    @Override
    public long getItemId(int position) {
        return productVariants.indexOf(getItem(position));
    }
}

但它的给定错误&#34;变量持有者是从内部类中访问的。需要宣布最终&#34;

这是给出错误的一行。

 holder.btnProductDetailMinus.setVisibility(View.VISIBLE);

如何解决这个问题?

4 个答案:

答案 0 :(得分:0)

将此行移出if (convertView == null)

holder = new ViewHolder();

答案 1 :(得分:0)

您可以声明final ViewHolder holder = null;而不是ViewHolder holder = null;

  1. 您可以将其设为全局变量

  2. 设置public static class ViewHolder而不是private class ViewHolder

答案 2 :(得分:0)

指定持有人最终变数:

final ViewHolder fHolder = holder;

然后在onClick:

fHolder.btnProductDetailMinus.setVisibility(View.VISIBLE);

答案 3 :(得分:0)

我遵循的方法之一是在Adapter类和onClick设置值和该集合中创建Map或List,并在适配器getView方法中检查集合值。 。

查看以下代码。

public class ProductVariantAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<ProductVariant> productVariants = new ArrayList<>();
    private Map<Integer, Boolean> visibilityMap = new HashMap<>();
    /*private view holder class*/
    private class ViewHolder {
        TextView productVariantName;
        TextView productVariantMrp;
        TextView productVariantSellPrice;
        Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
    }

    public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
        this.context = context;
        this.productVariants = productVariants;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_product_variant, null);
            holder = new ViewHolder();
            holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
            holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
            holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
            holder.btnProductDetailAddToCart = (Button)  convertView.findViewById(R.id.btnProductDetailAddToCart);
            holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
            holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
            holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
            holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (int)v.getTag();
                    Utility.displayToast("wonderful");
                    visibilityMap.put(position, true);
                    notifyDataSetChanged();
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.btnProductDetailAddToCart.setTag(position);

        ProductVariant productVariant = (ProductVariant) getItem(position);
        if (productVariant != null) {
            holder.productVariantName.setText(productVariant.getVariant().getVariantName());
            holder.productVariantMrp.setText(productVariant.getMrp().toString());
            holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
        } else {
            Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
        }
        if(visibilityMap.containsKey(position) && visibilityMap.get(position)){
           holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
        }else{
           holder.btnProductDetailMinus.setVisibility(View.GONE);
        }   

        return convertView;
    }

    @Override
    public int getCount() {
        return productVariants.size();
    }

    @Override
    public Object getItem(int position) {
        return productVariants.get(position);
    }

    @Override
    public long getItemId(int position) {
        return productVariants.indexOf(getItem(position));
    }
}