Home>

was an application with ListView. I rewar it under RecyclerView. C. CardView. . Previously, the lines from the list were removed through the context menu. Tried to fasten the context menu to RecyclerView. (For deletion) -It did not work ... Now try to remove through LongClick. . But if it turns out to delete the item, the list is not updated. For reading I use CursorLoader in a fragment.

Tell me how to organize removal correctly?

Recordadapter

Public Class Recordadapter Extends RecyclerView.adapter <
RecordadaPter.Viewholder >
 {
        CURSOR DATACURSOR;
        CONTEXT CONTEXT;
        Public Static Class Viewholder Extends RecyclerView.Viewholder {
            Public TextView Date;
            Public TextView Distance;
            Public imageView imageview;
            Public Viewholder (View V) {
                Super (V);
                Date= (textView) v.FindViewByid (R.ID.TVDATE);
                distance= (textView) v.findViewByid (R.ID.Tvtitle);
                imageview= (imageview) v.findViewByid (R.ID.IVIMG);
            }
        }
        Public Recordadapter (Activity MContext, Cursor Cursor) {
            datacursor= cursor;
            CONTEXT= MCONTEXT;
        }
        @OVerride
        Public Recordadapter.Viewholder OnCreateViewholder (ViewGroup Parent, Int ViewType) {
            View CardView= LayoutInflater.from (Parent.getContext ())
                    .inflate (R.Layout.Item, Parent, False);
            Return New Viewholder (CardView);
        }
        Public Cursor Swapcursor (CURSOR CURSOR) {
            if (datacursor== cursor) {
                RETURN NULL;
            }
            Cursor oldcursor= datacursor;
            this.datacursor= cursor;
            If (Cursor!= NULL) {
                this.notifyDataSetChanged ();
            }
            Return OldCursor;
        }
        @OVerride
        Public Void OnbindviewHolder (Viewholder Holder, Final Int Position) {
            DataCursor.MoveTopocation (Position);
            holder.ItemView.SetonClicklistener (new view.onclicklistener () {
                @OVerride
                Public Void OnClick (View V) {
                    if (Datacursor!= NULL) {
                        DataCursor.MoveTopocation (Position);
                        INTENT INTENT= NEW INTENT (CONTEXT, NOTE.CLASS);
                        long id= datacursor.getcolumnindex (db.column_id));
                        INTENT.PUTEXTRA ("ID", ID);
                        context.startactivity (INTENT);
                    }
                }
            });
            holder.ItemView.SetonLongClicklistener (new view.onlongclicklistener () {
                @OVerride
                Public Boolean OnlongClick (View V) {
                    Final alertdialog.builder Builder= new alertdialog.builder (V.GetContext ());
                    Builder.Settitle ("Delete")
                            .setMessage ("Delete?")
                            .setnegativeButton ("Cancel", new dialoginterface.onclicklistener () {
                                @OVerride
                                Public Void OnClick (DialogInterface Dialog, Int Which) {
                                }
                            })
                            .setpositiveTton ("OK", new dialoginterface.onclicklistener () {
                                @OVerride
                                Public Void OnClick (DialogInterface Dialog, Int Which) {
                                    //Here I wanted to delete the list item after what you need to update the list
                            DataCursor.MoveTopocation (Position);long id= datacursor.getcolumnindex (db.column_id)); DB NOTESDB= NEW DB (CONTEXT);
                            notesdb.open ();
                            notesdb.delrec (db.db_table_notes, id);
                            notesdb.close ();
                            notifydatasetchanged ();
                                }
                            });
                    Builder.Create (). show ();
                    RETURN TRUE;
                }
            });
            String date= datacursor.getcolumnindex (db.column_date);
            String title= datacursor.getcolumnindex (db.column_title);
            String Value= datacursor.getcolumnindex (db.column_color);
            SimpleDateFormat SDF= new SimpleDateFormat ("dd.mm.yyy");
            Date ResultDate= New Date (long.parselong (date));
            String SDFDATE= SDF.Format (ResultDate);
            if (title.equals (")) {
                title= context.getresources (). getstring (r.string.non_title);
            }
            holder.date.settext (SDFDATE);
            holder.distance.settext (title);
            GradientDrawable bgshape= (gradientDrawable) holder.imageview.getBackGround ();
            bgshape.setcolor (Integer.Parseint (Value));
        }
        @OVerride
        Public Long GetItemid (INT POSITION) {
            RETURN SUPER.GETIEMID (Position);
        }
        @OVerride
        Public int getItemcount () {
            Return (Datacursor== NULL)? 0: datacursor.getcount ();
        }
    }

It is better to delete "Laptop" elements without any menu

Style-72021-05-04 17:19:30
  • Answer # 1

    After deletion, write: NotifyDataSetChanged ();

    Added to OnClick deletion and NotifyDataSetChanged (); But the list is not updated immediately. Maybe the deletion itself is not true, or notifydatasetchanged (); Didn't put there? (corrected the code in the question, with changes made)

    Sergey Alexandrov2021-05-03 23:48:18

    @SergeyalExandrov Most likely, because the data is read from DataCursor, and the data is deleted via Notesdb. NotifyDataSetChanged is called, but the data in the cursor has not changed. Most likely you need to call the level above swapcursor

    hardsky2021-05-03 23:48:18