Android RecyclerView添加和删除项目
我的Recyclerview由光标填充。。然而,到目前为止,我还没有找到一种从recyclerview中实现添加或删除项目的方法 下面是我的适配器类:Android RecyclerView添加和删除项目,android,android-cursor,android-recyclerview,Android,Android Cursor,Android Recyclerview,我的Recyclerview由光标填充。。然而,到目前为止,我还没有找到一种从recyclerview中实现添加或删除项目的方法 下面是我的适配器类: class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> { Cursor curs; Context ctx; ViewHolder vh; CardView v;
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();
}
}
}
class NotesAdapter扩展了RecyclerView.Adapter{
光标光标;
上下文ctx;
视窗座;
卡德维尤五号;
公共类ViewHolder扩展了RecyclerView.ViewHolder{
公共文本查看注释标题、注释文本;
公共关系注释;
公共视图持有人(CardView v){
超级(五);
注:标题=(文本视图)v.findviewbyd(R.id.tvTitle);
注:文本=(文本视图)v.findviewbyd(R.id.tvText);
RLNote=(RelativeLayout)v.findViewById(R.id.note\u background);
}
}
公共NotesAdapter(上下文,游标c){
ctx=上下文;
curs=c;
}
@凌驾
public NotesAdapter.ViewHolder onCreateViewHolder(视图组父级、,
int视图类型){
v=(CardView)LayoutFlater.from(parent.getContext()).inflate(
R.layout.notes\u fragment\u custom,parent,false);
vh=新的视窗支架(v);
返回vh;
}
@凌驾
公共无效onBindViewHolder(最终视图持有人,最终整型位置){
//TODO自动生成的方法存根
光标移动位置(位置);
holder.Note\u Title.setText(curs.getString(curs
.getColumnIndex(MiroDatabase.KEY_NOTES_TITLE));
holder.Note\u Text.setText(curs.getString(curs
.getColumnIndex(MiroDatabase.KEY_NOTES_TEXT));
}
公共整数getItem(整数位置){
返回位置;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回super.getItemId(位置);
}
@凌驾
public int getItemCount(){
返回curs.getCount();
}
}
}
就像任何recyclerview适配器类一样,但加上游标
我在谷歌上没有找到这方面的任何结果
希望你能帮忙!谢谢:)在您的活动中,您可以:
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;
}
RecyclerView并不是专门用来处理游标的。在数据更改后,您必须使用光标重新查询。在查询时,必须将该游标转换为arraylist,然后跟踪在重新查询时更改的内容