我正在为我的android应用程序使用房间持久性库,当我在android房间持久性库中插入,然后想代表id选择表数据时,我想获取生成的最后一个id(自动生成主键)

@Entity
public class Task implements Serializable {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "task")
    private String task;

    @ColumnInfo(name = "desc")
    private String desc;

    @ColumnInfo(name = "finish_by")
    private String finishBy;

    @ColumnInfo(name = "finished")
    private boolean finished;


    /*
    * Getters and Setters
    * */
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getFinishBy() {
        return finishBy;
    }

    public void setFinishBy(String finishBy) {
        this.finishBy = finishBy;
    }

    public boolean isFinished() {
        return finished;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }
}


然后

@Dao
public interface TaskDao {

    @Query("SELECT * FROM task")
    List<Task> getAll();

    @Insert
    void insert(Task task);
    @Delete
    void delete(Task task);
    @Update
    void update(Task task);
}




private void saveTask() {
    final String sTask = editTextTask.getText().toString().trim();
    final String sDesc = editTextDesc.getText().toString().trim();
    final String sFinishBy = editTextFinishBy.getText().toString().trim();

    if (sTask.isEmpty()) {
        editTextTask.setError("Task required");
        editTextTask.requestFocus();
        return;
    }

    if (sDesc.isEmpty()) {
        editTextDesc.setError("Desc required");
        editTextDesc.requestFocus();
        return;
    }

    if (sFinishBy.isEmpty()) {
        editTextFinishBy.setError("Finish by required");
        editTextFinishBy.requestFocus();
        return;
    }

    class SaveTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            //creating a task
            Task task = new Task();
            task.setTask(sTask);
            task.setDesc(sDesc);
            task.setFinishBy(sFinishBy);
            task.setFinished(false);

            //adding to database
            DatabaseClient.getInstance(getApplicationContext()).getAppDatabase()
                    .taskDao()
                    .insert(task);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            finish();
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
            Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
        }
    }
    SaveTask st = new SaveTask();
    st.execute();
}

最佳答案

official documentation所述,可以通过指定@Insert函数具有返回类型来获得id。

因此,您需要按如下所示更改Dao代码以获取生成的ID,

@Insert
long insert(Task task);

09-04 03:15