回收站适配器中菜单的setOnMenuItemClickListener

时间:2018-12-20 18:30:24

标签: java android recycler-adapter popupmenu

由于考虑到我的发布,我可以在回收站的视图中实现包含元素的菜单,但是我的问题是访问菜单项的click事件,我发现它们是通过活动来完成的,但是我已经搜索了要在回收站适配器中执行此操作的信息,但找不到任何东西。我必须补充一点,我已经实现了它,但是即使这样我仍然遇到错误,希望您能告诉我我做错了什么或实现它的正确方法。

public class ViewHolderPedido extends RecyclerView.ViewHolder implements View.OnClickListener, PopupMenu.OnMenuItemClickListener {

        @BindView(R.id.txtNumeroPedido)
        TextView txtNumeroPedido;
        @BindView(R.id.txtFechaPedido)
        TextView txtFechaPedido;
        @BindView(R.id.txtSubTotalPedido)
        TextView txtSubTotal;
        @BindView(R.id.txtEstadpoPedidoList)
        TextView txtEstadoPedido;
        @BindView(R.id.txtNombreProductoPedidoList)
        TextView txtNombreProduct;
        @BindView(R.id.imgProductoPedidoList)
        ImageView imgProducto;
        @BindView(R.id.menu)
        ImageView mImgMenu;
        @BindView(R.id.content_order)
        LinearLayout mContentOrder;

        PopupMenu popupMenu;
        PedidoModelo pedidoModelo;

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.menu:
                    popupMenu = new PopupMenu(mContext, view);
                    popupMenu.getMenuInflater().inflate(R.menu.menu_cancel, popupMenu.getMenu());
                    popupMenu.show();
                    break;
                case R.id.content_order:
                    mListener.OnItemClick(view, pedidoModelo, getAdapterPosition(), true);
                    break;
            }
        }

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return false;
        }

        public ViewHolderPedido(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mImgMenu.setOnClickListener(this);
            mContentOrder.setOnClickListener(this);
            popupMenu.setOnMenuItemClickListener(this);
        }

        protected void bind(PedidoModelo model) {
            this.pedidoModelo = model;

            txtFechaPedido.setText(pedidoModelo.getFecha());
            txtSubTotal.setText(PrecioFormater.formatoPrecio(pedidoModelo.getPrecioTotal()));
            txtNumeroPedido.setText(pedidoModelo.getNumeroOrden());
            txtEstadoPedido.setText(pedidoModelo.getEstado());
            txtNombreProduct.setText(pedidoModelo.getNombreProducto());
            Picasso.with(mContext).load(pedidoModelo.getUrlImagen()).into(imgProducto);
            switch (pedidoModelo.getCodigo_estado()) {
                case OrderStatus.PENDING:
                    mImgMenu.setVisibility(View.VISIBLE);
                    break;
                case OrderStatus.COMPLETE:
                    mImgMenu.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    public interface OnItemClickListener {

        boolean OnItemClick(View view, PedidoModelo item, int position, boolean longPress);

    }
}

加载回收站时出现此错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.PopupMenu.setOnMenuItemClickListener(android.widget.PopupMenu$OnMenuItemClickListener)' on a null object reference

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,碰巧我在构造函数中声明了setOnMenuItemClickListener方法,这是在加载菜单时实例化它的正确做法。在这里,我离开解决方案。

@Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.menu:
                    popupMenu = new PopupMenu(mContext, view);
                    popupMenu.getMenuInflater().inflate(R.menu.menu_cancel, popupMenu.getMenu());
                    popupMenu.setOnMenuItemClickListener(this);
                    popupMenu.show();
                    break;
                case R.id.content_order:
                    mListener.OnItemClick(view, pedidoModelo, getAdapterPosition(), true);
                    break;
            }
        }

请确保您的声明正确无误。我把自己的名字寄给了我,所以我把它弄成空指针

https://mysqlhighavailability.com/mysql-group-replication-auto-increment-configuration-handling/