Java 如何将从存储库自动生成的ID返回到MainActivity?

Java 如何将从存储库自动生成的ID返回到MainActivity?,java,android,android-studio,android-room,Java,Android,Android Studio,Android Room,我有一个无法解决的大问题。我正在开发一个“提醒应用程序”,允许你插入一个备忘录,并在该做的时候通知你。我在Android中使用Room,当我插入新任务时,我想使用插入任务的id作为管理通知的id。我有TaskViewModel和TaskRepository。这是我写的代码: 在我的道中: @Insert long insert(Task task); Task task = new Task(title, data_calendario, time, priority, type, note,

我有一个无法解决的大问题。我正在开发一个“提醒应用程序”,允许你插入一个备忘录,并在该做的时候通知你。我在Android中使用Room,当我插入新任务时,我想使用插入任务的id作为管理通知的id。我有TaskViewModel和TaskRepository。这是我写的代码:

在我的道中:

@Insert
long insert(Task task);
Task task = new Task(title, data_calendario, time, priority, type, note, "pending");
taskViewModel.insert(task);
public void insert(Task task){
    repository.insert(task);
}
public void insert(Task task){
    new InsertTaskAsyncTask(taskDao).execute(task);
}

private class InsertTaskAsyncTask extends AsyncTask<Task, Void, Long>{
    private TaskDao taskDao;


    private InsertTaskAsyncTask(TaskDao taskDao){
        this.taskDao = taskDao;
    }


    @Override
    protected Long doInBackground(Task... tasks) {

      Long test = taskDao.insert(tasks[0]);

      System.out.println("Stampa repository BUONO: "+test); //here I have the id that I need (I think..)

     return test;
    }

    @Override
    protected void onPostExecute(Long test) {
        super.onPostExecute(test);
        id = test;
    }
}
在我的主要活动中:

@Insert
long insert(Task task);
Task task = new Task(title, data_calendario, time, priority, type, note, "pending");
taskViewModel.insert(task);
public void insert(Task task){
    repository.insert(task);
}
public void insert(Task task){
    new InsertTaskAsyncTask(taskDao).execute(task);
}

private class InsertTaskAsyncTask extends AsyncTask<Task, Void, Long>{
    private TaskDao taskDao;


    private InsertTaskAsyncTask(TaskDao taskDao){
        this.taskDao = taskDao;
    }


    @Override
    protected Long doInBackground(Task... tasks) {

      Long test = taskDao.insert(tasks[0]);

      System.out.println("Stampa repository BUONO: "+test); //here I have the id that I need (I think..)

     return test;
    }

    @Override
    protected void onPostExecute(Long test) {
        super.onPostExecute(test);
        id = test;
    }
}
在我拥有的TaskViewModel中:

@Insert
long insert(Task task);
Task task = new Task(title, data_calendario, time, priority, type, note, "pending");
taskViewModel.insert(task);
public void insert(Task task){
    repository.insert(task);
}
public void insert(Task task){
    new InsertTaskAsyncTask(taskDao).execute(task);
}

private class InsertTaskAsyncTask extends AsyncTask<Task, Void, Long>{
    private TaskDao taskDao;


    private InsertTaskAsyncTask(TaskDao taskDao){
        this.taskDao = taskDao;
    }


    @Override
    protected Long doInBackground(Task... tasks) {

      Long test = taskDao.insert(tasks[0]);

      System.out.println("Stampa repository BUONO: "+test); //here I have the id that I need (I think..)

     return test;
    }

    @Override
    protected void onPostExecute(Long test) {
        super.onPostExecute(test);
        id = test;
    }
}
在TaskRepository中,我有:

@Insert
long insert(Task task);
Task task = new Task(title, data_calendario, time, priority, type, note, "pending");
taskViewModel.insert(task);
public void insert(Task task){
    repository.insert(task);
}
public void insert(Task task){
    new InsertTaskAsyncTask(taskDao).execute(task);
}

private class InsertTaskAsyncTask extends AsyncTask<Task, Void, Long>{
    private TaskDao taskDao;


    private InsertTaskAsyncTask(TaskDao taskDao){
        this.taskDao = taskDao;
    }


    @Override
    protected Long doInBackground(Task... tasks) {

      Long test = taskDao.insert(tasks[0]);

      System.out.println("Stampa repository BUONO: "+test); //here I have the id that I need (I think..)

     return test;
    }

    @Override
    protected void onPostExecute(Long test) {
        super.onPostExecute(test);
        id = test;
    }
}
它是在insert方法之后从MainActivity调用的。 问题是,第一次运行应用程序并创建任务时,此变量中的值始终为0。我如何解决这个问题

这可能会对你有所帮助

基于文档(代码段下方)

@Insert
注释注释的方法可以返回:

  • long
    用于单次插入操作
  • long[]
    long[]
    List
    用于多个插入操作
  • void
    如果您不关心插入的id

是的,我读过,但我不知道如何通过存储库、ViewModel传递ID,并将值保存在MainActivity中。有人能给我看一个代码示例吗?我做错了什么?