2

i have been trying to implement the swipe to delete functionality for a list i have which is done by a recyclerview, right now i need to find a way to delete the item from the realm database using only the position at the recyclerview here's the code

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);

and this is the adapter class, i need to implement the removeItem method

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();
}

}

i keep getting this error and the app crashes when i try to swipe an item from the 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)

the line at List.java:56 is this one

ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleCallback);
  • Is this on API 19 or older? – EpicPandaForce Jan 09 '18 at 01:57
  • no, min sdk is 21 –  Jan 09 '18 at 09:52
  • It *looks* like you have two different versions of support library in your code, you should do a `./gradlew :app:dependencies` to see where they come from – EpicPandaForce Jan 09 '18 at 10:37
  • i have both those underlined with red at the gradle file but i dont see any actual error compile 'com.android.support:recyclerview-v7:25.0.1' compile 'com.android.support:design:25.0.1' –  Jan 09 '18 at 10:43

0 Answers0