使用realm数据库滑动以从recyclerview中删除

时间:2018-01-08 22:06:37

标签: android android-studio android-recyclerview realm recyclerview-layout

我一直在尝试实现滑动以删除我所拥有的列表的功能,这是由recyclerview完成的,现在我需要找到一种方法,仅使用recyclerview中的位置从域数据库中删除项目 这是代码

ItemTouchHelper.SimpleCallback simpleCallback=new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int id = (int) viewHolder.itemView.getTag();
            adapter.removeItem(id);
        }
    };
    ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleCallback);
    mItemTouchHelper.attachToRecyclerView(mRecyclerView);

这是适配器类,我需要实现removeItem方法

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private LayoutInflater layoutInflater;
java.util.List<UserInfo> data= Collections.emptyList();
//ArrayList<UserInfo> arrayList;
public RelativeLayout viewForeground;
public Adapter(Context context, java.util.List<UserInfo> data){
    layoutInflater=LayoutInflater.from(context);
    this.data=data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=layoutInflater.inflate(R.layout.list_item , parent,false);
    MyViewHolder holder=new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    UserInfo current=data.get(position);
    holder.username.setText("Username: "+current.username);
    holder.password.setText("Password: "+current.password);
    holder.type.setText("Type: "+current.type);
}

@Override
public int getItemCount() {
    return data.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView username,password,type;
    public MyViewHolder(View itemView) {
        super(itemView);
        username= (TextView) itemView.findViewById(R.id.username_TV);
        password= (TextView) itemView.findViewById(R.id.password_TV);
        type= (TextView) itemView.findViewById(R.id.type_TV);
    }
}
public void removeItem(int position) {
    data.remove(position);
    notifyItemRemoved(position);
}
public void setFilter(List<UserInfo> newList){
    data=new ArrayList<>();
    data.addAll(newList);
    notifyDataSetChanged();
}

}

我一直收到此错误,当我尝试从recyclerview中刷一个项目时应用程序崩溃

E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.NoClassDefFoundError: Failed resolution of: 
Landroid/support/v4/animation/AnimatorListenerCompat;
                    at com.example.omara.lifesaver.List.onCreate(List.java:56)

List.java:56上的这一行是

ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleCallback);

0 个答案:

没有答案