RecyclerView添加和删除项目

时间:2015-07-01 13:45:32

标签: android android-cursor android-recyclerview

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

这是我的Adapter类:

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适配器类一样,但加上游标......

我没有在谷歌上找到任何结果

希望你帮忙!谢谢:))

1 个答案:

答案 0 :(得分:1)

在您的活动中,您可以:

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

在NotesAdapter类中,添加函数:

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