Update some specific field of an entity in android Room

前端 未结 7 1625
臣服心动
臣服心动 2020-11-29 16:45

I am using android room persistence library for my new project. I want to update some field of table. I have tried like in my Dao -

// Method 1         


        
7条回答
  •  执念已碎
    2020-11-29 17:47

    If you need to update user information for a specific user ID "x",

    1. you need to create a dbManager class that will initialise the database in its constructor and acts as a mediator between your viewModel and DAO, and also .
    2. The ViewModel will initialize an instance of dbManager to access the database. The code should look like this:

         @Entity
          class User{
          @PrimaryKey
          String userId;
          String username;
          }
      
          Interface UserDao{
          //forUpdate
          @Update
          void updateUser(User user)
          }
      
          Class DbManager{
          //AppDatabase gets the static object o roomDatabase.
          AppDatabase appDatabase;
          UserDao userDao;
          public DbManager(Application application ){
          appDatabase = AppDatabase.getInstance(application);
      
          //getUserDao is and abstract method of type UserDao declared in AppDatabase //class
          userDao = appDatabase.getUserDao();
          } 
      
          public void updateUser(User user, boolean isUpdate){
          new InsertUpdateUserAsyncTask(userDao,isUpdate).execute(user);
          }
      
      
      
          public static class InsertUpdateUserAsyncTask extends AsyncTask {
      
      
           private UserDao userDAO;
           private boolean isInsert;
      
           public InsertUpdateBrandAsyncTask(BrandDAO userDAO, boolean isInsert) {
             this. userDAO = userDAO;
             this.isInsert = isInsert;
           }
      
           @Override
           protected Void doInBackground(User... users) {
             if (isInsert)
          userDAO.insertBrand(brandEntities[0]);
             else
          //for update
          userDAO.updateBrand(users[0]);
          //try {
          //  Thread.sleep(1000);
          //} catch (InterruptedException e) {
          //  e.printStackTrace();
          //}
             return null;
           }
            }
          }
      
           Class UserViewModel{
           DbManager dbManager;
           public UserViewModel(Application application){
           dbmanager = new DbMnager(application);
           }
      
           public void updateUser(User user, boolean isUpdate){
           dbmanager.updateUser(user,isUpdate);
           }
      
           }
      
      
      
      
      Now in your activity or fragment initialise your UserViewModel like this:
      
      UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
      

      Then just update your user item this way, suppose your userId is 1122 and userName is "xyz" which has to be changed to "zyx".

      Get an userItem of id 1122 User object

    User user = new user();
     if(user.getUserId() == 1122){
       user.setuserName("zyx");
       userViewModel.updateUser(user);
     }
    

    This is a raw code, hope it helps you.

    Happy coding

提交回复
热议问题