Fringo
Fringo

Reputation: 313

RecyclerView Add and delete items

My Recyclerview is filled by a cursor.. however i haven't found out a way to implement add or delete items from the recyclerview so far

Here's my Adapter class :

class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> {


        Cursor curs;
        Context ctx;
        ViewHolder vh;
        CardView v;


        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView Note_Title, Note_Text;
            public RelativeLayout RLNote;

            public ViewHolder(CardView v) {
                super(v);
                Note_Title = (TextView) v.findViewById(R.id.tvTitle);
                Note_Text = (TextView) v.findViewById(R.id.tvText);
                RLNote = (RelativeLayout) v.findViewById(R.id.note_background);
            }
        }

        public NotesAdapter(Context context, Cursor c) {
            ctx = context;
            curs = c;
        }

        @Override
        public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                int viewType) {
            v = (CardView) LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.notes_fragment_custom, parent, false);
            vh = new ViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            // TODO Auto-generated method stub
            curs.moveToPosition(position);

            holder.Note_Title.setText(curs.getString(curs
                    .getColumnIndex(MiroDatabase.KEY_NOTES_TITLE)));

                holder.Note_Text.setText(curs.getString(curs
                        .getColumnIndex(MiroDatabase.KEY_NOTES_TEXT)));


        }

        public int getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return super.getItemId(position);
        }

        @Override
        public int getItemCount() {
            return curs.getCount();
        }
    }

}

Just like any recyclerview adapter class, but coupled with a cursor...

i didn't find on google any results for this

Hope you help! Thanks :)

Upvotes: 1

Views: 374

Answers (1)

Omar Farhat
Omar Farhat

Reputation: 668

In your activity you can :

TheDatabase MD;
NotesAdapter AA;
MD.close();
MD.open();
AA.ChangeCursor(c);
AA.notifyDataSetChanged();

And in NotesAdapter class, add the function :

public void ChangeCursor(Cursor c) {
    // TODO Auto-generated method stub
    curs = c;
}

Upvotes: 1

Related Questions