【问题标题】:RecyclerView Add and delete itemsRecyclerView 添加和删除项目
【发布时间】:2015-09-18 17:15:19
【问题描述】:

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

这是我的适配器类:

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

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

希望你能帮忙!谢谢:)

【问题讨论】:

  • RecyclerView 并不能真正与游标一起使用。使用游标,您必须在数据更改后重新查询。当您查询时,您将不得不将该光标变成一个数组列表,然后跟踪您重新查询时发生的变化

标签: android android-cursor android-recyclerview


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多