RecyclerView 添加和删除项目

RecyclerView Add and delete items

我的 Recyclerview 由光标填充..但是我还没有找到一种方法来实现从 recyclerview 添加或删除项目

这是我的适配器 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();
        }
    }

}

就像任何 recyclerview 适配器一样 class,但加上了游标...

我在 google 上没有找到与此相关的任何结果

希望对您有所帮助!谢谢:)

在您的 activity 中,您可以:

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

并在 NotesAdapter class 中添加函数 :

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