realm / realm-android-adapters

Adapters for combining Realm Java with Android UI components and framework classes
realm.io
Apache License 2.0
414 stars 134 forks source link

Remove first item from RealmRecyclerView #127

Closed scifinder closed 6 years ago

scifinder commented 7 years ago

Hello! I use this code to delete an item from RealmRecyclerView:

public void onBindViewHolder(NotesViewHolder holder, int position) {
    final RealmElement obj = getItem(position);
    if(obj != null) {

        ...

        holder.deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ...

                Realm realm = Realm.getDefaultInstance();
                realm.beginTransaction();
                obj.deleteFromRealm();
                realm.commitTransaction();
                notifyDataSetChanged();

            }
        });
    }
}

This works fine for items in the middle and at the end of the list. However, if I delete the first item, the latter, rather than the second one, becomes its place. For example: before removing, it was A - B - C - D, after removal was D - B - C.

Verteletsky commented 7 years ago

I solved the problem.

MyRecyclerViewAdapter(OrderedRealmCollection data) { super(data, true); setHasStableIds(true); }

Write me, verteletsky@bitrix24.ru

scifinder commented 7 years ago

@Verteletsky I had it from the beginning.

    MyRecyclerViewAdapter(OrderedRealmCollection<RealmElement> data, Context ctx) {
        super(data, true);
        this.mContext = ctx;
        setHasStableIds(true);
    }

But it don't work for me.

Verteletsky commented 7 years ago

@scifinder Go telegram, take my code from bitbucket

public RvRealmAdapter(RealmList list, Context context, OnStartDragListener dragStartListener, Realm realm) { setHasStableIds(true); currentDate = new Date(); this.list = list; this.context = context; this.dragStartListener = dragStartListener; this.realm = realm; }

В итоге я ушел от их адаптера и пишу свой.