how to remove all items from RecyclerView on toolbar icon delete in android

290 views Asked by At

Hello friend a lot of search but not working plz help me. my probem is delete all history from adaptor using on toolbar delete icon how to remove adaptor data below code first show adaptor and mainactivity public void removehistory(View view) button click to remove items from adaptor how to solve this problem sorry for bad english and advance thanks

HistoryAdaptor

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

    private ArrayList<Object> data;
    private GenCallback<HistoryModel> clickListener;
    private static final int NATIVE_AD = 1;
    private static final int HISTORY_ITEM = 2;
    private LayoutInflater inflater;
    //    private Button removedata;



    public HistoryAdapter() {
        this.data = new ArrayList<>();
        inflater = (LayoutInflater) App.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case NATIVE_AD:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_unified, null, false);
                return new NativeAdHolder(view);
            case HISTORY_ITEM:
            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history, null, false);
                return new ViewHolder(view);
        }

//        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history, null, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case NATIVE_AD:
                ((NativeAdHolder) holder).bind((UnifiedNativeAd) data.get(position));
                break;
            case HISTORY_ITEM:
            default:
                ((ViewHolder) holder).bind((HistoryModel) data.get(position));
                break;
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (data.get(position) instanceof UnifiedNativeAd) {
            return NATIVE_AD;
        } else {
            return HISTORY_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void addItem(Object obj, int pos) {
        this.data.add(pos, obj);
        notifyItemInserted(pos);
    }

    public void updateData(ArrayList<HistoryModel> list) {
        this.data.clear();
        this.data.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        if (this.data != null && this.data.size() != 0) {
            this.data.clear();
            notifyDataSetChanged();
        }
    }

    public void setClickListener(GenCallback<HistoryModel> clickListener) {
        this.clickListener = clickListener;
    }

    public void showStarredOnly() {

        /*Iterator<Object> iterator = data.iterator();
        int pos = 0;
        while (iterator.hasNext()) {
            if (iterator.next() instanceof HistoryModel) {
                HistoryModel historyModel = (HistoryModel) iterator.next();
                if (!historyModel.isStarred()) {
                    iterator.remove();
//                notifyItemRemoved(pos);
                }
            }
            pos++;
        }
        notifyDataSetChanged();*/
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvFromLang)
        TextView tvFromLang;

        @BindView(R.id.tvFromText)
        TextView tvFromText;

        @BindView(R.id.tvToLang)
        TextView tvToLang;

        @BindView(R.id.tvToText)
        TextView tvToText;

        @BindView(R.id.ivStar)
        ImageView ivStar;

        @BindView(R.id.llMain)
        CardView llMain;

        @BindView(R.id.tvDate)
        TextView tvDate;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            
            llMain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clickListener != null) {
                        clickListener.onCallback((HistoryModel) data.get(getAdapterPosition()));
                    }
                }
            });

            llMain.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    int pos = getAdapterPosition();
                    HistoryModel historyModel = (HistoryModel) data.get(pos);
                    boolean isStar = historyModel.isStarred();
                    String from = historyModel.getFromLang();
                    String to = historyModel.getToLang();

                  

                    Dialogs.INSTANCE.showActionsDialog(llMain.getContext(), isStar, from, to, new HistoryActionCallback() {
                        @Override
                        public void onClickCopyFrom() {
                            Utils.copyText(historyModel.getFromText());
                            Logger.logFabric(Constants.Events.COPY_FROM);
                        }

                        @Override
                        public void onClickCopyTo() {
                            Utils.copyText(historyModel.getToText());
                            Logger.logFabric(Constants.Events.COPY_TO);
                        }

                        @Override
                        public void onClickStar() {
                            Logger.logFabric(Constants.Events.MARKED_AS_FAV);
                            historyModel.setStarred(!isStar);
                            notifyItemChanged(pos);
                            RoomRepository.getNew().updateHistoryItem(historyModel);
//                            notifyDataSetChanged();
                        }

                        @Override
                        public void onClickDelete() {
                            String message = "Are you sure you want to delete?";
                            Dialogs.INSTANCE.showConfirmationDialog(view.getContext(), message, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    RoomRepository.getNew().deleteHistoryRecord(historyModel.getTimeStamp());
                                    data.remove(pos);
                                    notifyItemRemoved(pos);
                                    Dialogs.INSTANCE.dismissDialog();
                                    Logger.logFabric(Constants.Events.ITEM_DELETED);
                                }
                            });
                        }
                    });
                    return false;
                }
            });
        }

        public void bind(HistoryModel item) {
            StringBuilder builder = new StringBuilder();
            builder.append(item.getFromLang()).append("(").append(item.getFromCode()).append(")");
            tvFromLang.setText(builder);
            tvFromText.setText(item.getFromText());

            builder.setLength(0);
            builder.append(item.getToLang()).append("(").append(item.getToCode()).append(")");
            tvToLang.setText(builder);
            tvToText.setText(item.getToText());

            String date = Utils.formatDate(item.getTimeStamp(), "EEE, MMM d, yyyy");
            tvDate.setText(date);
            ivStar.setVisibility(item.isStarred() ? View.VISIBLE : View.GONE);

        }

MainActivity

public class HistoryActivity extends AppCompatActivity {
    private HistoryAdapter mAdapter;
    private ArrayList<Object> data;
    private Activity mCurrentActivity;
    private RecyclerView rvHistory;
    private Button clearh;
    private GenCallback<HistoryModel> clickListener;
    private ArrayList<HistoryModel> datamodel = new ArrayList<>();
    private ArrayList<String> arrayList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        rvHistory = findViewById(R.id.rvHistory);
        ImageView imgb = findViewById(R.id.imgback);

        initAdapter();
        getAdapter();

        imgb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HistoryActivity.super.onBackPressed();
            }
        });
        
    }

    private void getAdapter() {
        RoomRepository.getNew().getAllHistory(mCallback);
    }

    private void initAdapter() {
        mAdapter = new HistoryAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mCurrentActivity);
        rvHistory.setLayoutManager(layoutManager);
        rvHistory.setAdapter(mAdapter);

    }

    private IRoomDataHandler mCallback = new RoomDataHandler() {

        @Override
        public void onGetAllHistory(ArrayList<HistoryModel> list) {
//            if (isSafe()) {
            mAdapter.updateData(list);
//                showLoader(false);
//                checkForNoData();
//                if (AdUtils.getInstance().getNativeAd() != null) {
//
//                }
        }


        @Override
        public void onError(String error) {
//            showLoader(false);
//            checkForNoData();
        }
    };


    public void removehistory(View view) {


//problem is here 
       
            mAdapter.notifyItemRemoved(i);
       


        }

}
1

There are 1 answers

0
Purvesh Dodiya On

call clear() method from on click oh that button like this.

public void removehistory(View view) {


//problem is here 
       
            mAdapter.clear()           


        }