当我点击listview的一个按钮时,另一个按钮也点击了?我怎么能阻止它

时间:2016-02-05 08:48:25

标签: android listview onclicklistener

我有一个 listview 有这么多按钮。当我点击listview的一个按钮时,另一个按钮也点击了?我怎么能阻止它。

请解决这个问题。 给我任何技巧如何在列表视图中停止另一个按钮。

这是我的代码

private class MenuItemsAdapter extends ArrayAdapter<MenuItem> {

    private static final String TAG = "MenuItemsAdapter";

    public MenuItemsAdapter(Context context, List<MenuItem> menuItems) {
        super(context, 0, menuItems);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    final MenuItem   menuItem = getItem(position);



        View view = convertView;
        final ViewHolder viewHolder;
        LayoutInflater inflater;

            inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.menu_item, parent, false);
            viewHolder = new ViewHolder();
    //      viewHolder.half = (TextView) view.findViewById(R.id.half);
            viewHolder.name = (TextView) view.findViewById(R.id.name);
            viewHolder.description = (TextView) view.findViewById(R.id.description);
            viewHolder.price = (TextView) view.findViewById(R.id.price);
            viewHolder.add = (Button) view.findViewById(R.id.add);
            viewHolder.selectedView = view.findViewById(R.id.selectedView);
            viewHolder.remove = (Button) view.findViewById(R.id.remove);
            viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
            viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);

            view.setTag(viewHolder);



        try
        {






        viewHolder.name.setText(menuItem.name);
        viewHolder.description.setText(menuItem.description);

        viewHolder.price.setText(String.valueOf(menuItem.price));



          viewHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mApplication.createNewCartIfPossibleAndAskIfNot(
                        mActivity, mRestaurant,
                        new MainApplication.OnCreateCartListener() {
                            @Override
                            public void onCreateCart(Cart cart) {


                                            cart.addOne(menuItem);

                                            updateItemFromCart(menuItem, viewHolder);
                                            updateCart();


                            }


                        });

            }
        });


        viewHolder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!mApplication.isCartCreated()) {
                    return;
                }
                mApplication.getCart().removeOne(menuItem);

                updateItemFromCart(menuItem, viewHolder);
                updateCart();
            }
        });

        }catch(NullPointerException e){e.printStackTrace();}
        return view;
    }


    private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) {


        if (!mApplication.isCartCreated()) {
            return;
        }
        int quantity = mApplication.getCart().getNOfItemsOfType(menuItem);
        if (quantity > 0) {

            viewHolder.selectedView.setVisibility(View.VISIBLE);



        } else {

            viewHolder.selectedView.setVisibility(View.GONE);

        }

        viewHolder.quantity.setText(String.valueOf(quantity));

        viewHolder.total.setText(String.valueOf(quantity
                * menuItem.price));

    }

    class ViewHolder {
        TextView name;
        TextView description;
        TextView price;
        Button add;
        View selectedView;
        Button remove;
        TextView total;
        TextView quantity;
        TextView half;

    }

}

1 个答案:

答案 0 :(得分:0)

我可以想到你缺少使用适配器内的静态视图持有者的循环适配器实现。同样,这是基于文本和我过去经验的假设答案。更详细的调查需要代码研究。

您可以对视图进行空检查

    final MenuItem   menuItem = getItem(position);
    View view = convertView;
    final ViewHolder viewHolder;

if (convertView == null) {

    LayoutInflater inflater;

        inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.menu_item, parent, false);
        viewHolder = new ViewHolder();
//      viewHolder.half = (TextView) view.findViewById(R.id.half);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
        viewHolder.price = (TextView) view.findViewById(R.id.price);
        viewHolder.add = (Button) view.findViewById(R.id.add);
        viewHolder.selectedView = view.findViewById(R.id.selectedView);
        viewHolder.remove = (Button) view.findViewById(R.id.remove);
        viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
        viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
        view.setTag(viewHolder);
}else{
    viewHolder= (ViewHolder)convertView.getTag(); 
}