Soy nuevo en Android, actualmente estoy trabajando en implementaciones de recyclerView. El problema es que quiero eliminar un elemento después de que se haya llamado a onCreateContextMenu. Tengo algunos problemas para obtener la referencia correcta a mi opción de eliminación.

Clase de titular

  public class UploadDrugsHolder extends RecyclerView.ViewHolder  implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {
        //OUR VIEWS
        UploadDrugsAdapter uploadDrugsAdapter;
        TextView drugName;
        ImageView drugImage;
        ItemClickListener itemClickListener;
        ArrayList<UploadedDrugs> drugs;
        public UploadDrugsHolder(View itemView) {
            super(itemView);
            this.drugName= (TextView) itemView.findViewById(R.id.shopName);
            this.drugImage= (ImageView) itemView.findViewById(R.id.model_menu);
            itemView.setOnClickListener(this);
            itemView.setOnCreateContextMenuListener(this);
        }

        public void setItemClickListener(ItemClickListener itemClickListener) {
            this.itemClickListener=itemClickListener;
        }

        @Override
        public void onClick(View v) {
            this.itemClickListener.onItemClick(v,getLayoutPosition());
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuItem myActionItem = menu.add("delete");
            myActionItem.setOnMenuItemClickListener(this);
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
// what do i need to write here?
            Toast.makeText(itemView.getContext(), "Drug deleted successfully", Toast.LENGTH_SHORT).show();
            return false;
        }

    }
0
Bennykeys 15 nov. 2017 a las 21:50

2 respuestas

La mejor respuesta

Siempre pongo adaptador y soporte en el mismo archivo.
Deberías hacer así:

public class UploadedDrugsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<UploadedDrugs> dataList = new ArrayList<>();
    private OnClickUploadedDrugs event;

    public UploadedDrugsAdapter (List<UploadedDrugs> dataList, OnClickUploadedDrugs event) {
        this.dataList = dataList;
        this.event = event;
    }

    public interface OnClickUploadedDrugs{
        void onClickUploadedDrugs(UploadedDrugs uploadedDrugs);
    }

    public void changeDataSet(List<UploadedDrugs> dataSet){
        dataList = dataSet;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.your_xml_file, parent, false);

        return new ViewHolderUploadedDrugs(view);
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        try {
            final UploadedDrugs model = dataList.get(position);
            final ViewHolderUploadedDrugs vh = (ViewHolderUploadedDrugs) holder;

            // configure view values

            //Event
            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(event != null){
                        event.onClickUploadedDrugs(model);
                    }
                }
            });
        }catch (Exception e){
            Log.e(getClass().getSimpleName(), "Erro ao exibir informaçeõs", e);
        }
    }

    @Override
    public int getItemCount() {
        return (null != dataList ? dataList.size() : 0);
    }

    static class ViewHolderUploadedDrugs extends RecyclerView.ViewHolder {

        TextView drugName;
        ImageView drugImage;


        ViewHolderUploadedDrugs(View itemView) {
            super(itemView);
            this.drugName= (TextView) itemView.findViewById(R.id.shopName);
            this.drugImage= (ImageView) itemView.findViewById(R.id.model_menu);
        }
    }

    public List<UploadedDrugs> getDataList() {
        return dataList;
    }

    public void setEvent(OnClickUploadedDrugs event) {
        this.event = event;
    }
}
0
Maicon Hellmann 15 nov. 2017 a las 21:02

Algo así como:

drugs.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, drugs.size());
0
JMcQueen 15 nov. 2017 a las 18:59