猿问

如何从房间数据库中删除回收者视图项目

我正在使用 MVVM 架构模型创建一个应用程序。我在 MainActivity 中有回收器视图,单击回收器视图项目中的删除按钮时,它应该从房间数据库中删除。我知道可以在适配器类中删除项目,但因为我正在使用MVVM模型我想在Repository类中进行删除操作。


这是我的代码如下:


UserDao.java


@Dao

public interface UserDao {


@Insert(onConflict = OnConflictStrategy.REPLACE)

void Insert(User... users);


@Query("SELECT * FROM Users")

LiveData<List<User>> getRoomUsers();


@Delete

void Delete(User... user);

}   

UserAdapter.java


public class UserAdapter extends 

RecyclerView.Adapter<UserAdapter.ViewHolder> {


List<User> userList;

Context context;


public UserAdapter(List<User> userList, Context context) {

    this.userList = userList;

    this.context = context;

}


@NonNull

@Override

public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row_layout,parent,false);


    ViewHolder viewHolder = new ViewHolder(v);


    return viewHolder;

}


@Override

public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, int position) {


    final User users = userList.get(position);


    holder.row_name.setText(users.getName());

    holder.row_age.setText(users.getAge());


    holder.delete.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {



        }

    });

}


@Override

public int getItemCount() {

    return userList.size();

}


public void setUserList(List<User> userList) {

    this.userList = userList;

    notifyDataSetChanged();

}


public class ViewHolder extends RecyclerView.ViewHolder {


    TextView row_name,row_age;

    ImageView delete;


    public ViewHolder(@NonNull View itemView) {

        super(itemView);


        row_name = itemView.findViewById(R.id.row_name);

        row_age = itemView.findViewById(R.id.row_age);

        delete = itemView.findViewById(R.id.delete);

      }

   }

}


ITMISS
浏览 106回答 2
2回答

倚天杖

首先,UserViewModel像在 中那样在您的适配器类中进行初始化MainActivity,然后调用 delete 函数。holder.delete.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;userModel.deleteItem(users);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });在类中添加此功能UserModel。public void deleteItem(User user) = repo.deleteItem(user);在UserRepository课堂上,调用Delete函数。public void deleteItem(User user) {&nbsp; &nbsp; &nbsp; &nbsp; userDb.userDao().Delete(user);&nbsp;}

胡子哥哥

尝试使用实时数据观察数据库的变化,无论何时执行任何操作,添加或删除(在数据库中),在您的观察者中为回收者视图刷新该数据为您的适配器设置点击监听器public interface OnListInteractionListener {&nbsp; &nbsp; &nbsp; &nbsp; // TODO: Update argument type and name&nbsp; &nbsp; &nbsp; &nbsp; void onListInteraction(User user);&nbsp; &nbsp; }在适配器类private final OnListInteractionListener mListener;&nbsp; &nbsp; public UserAdapter(List<User> users, OnListInteractionListener listener,Context context) {&nbsp; &nbsp; &nbsp; &nbsp; mListener = listener;&nbsp; &nbsp; }并在视图中绑定持有人holder.delete.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {mListener.onListInteraction(userList.get(position))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });在您的活动/片段中实现该侦听器,您可以从中访问 Viewmodel 和 repo
随时随地看视频慕课网APP

相关分类

Java
我要回答