Android Room数据库,检索输入的最新记录的特定值 [英] Android Room Database, retrieve specific value of the latest record entered

查看:382
本文介绍了Android Room数据库,检索输入的最新记录的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Android会议室数据库创建了我的应用程序,我正在填写学生的记录,我想在选择学生的姓名时从会议室数据库中检索输入的最新记录的页码. 这是我的下面的代码
DOA

I have used Android Room Database to create my app, I am filling records of students, I want to retrieve page number of the latest record that I have entered from the Room Database when I select student's name. This is my code below
DOA

@Dao
public interface NewRecordDAO {

    @Insert(onConflict = REPLACE)
    public void addRecord(NewRecord... newRecord);

    @Update(onConflict = REPLACE)
    public void updateRecord(NewRecord newRecord);

    @Delete
    public void deleteRecord(NewRecord newRecord);

    @Query("DELETE FROM newRecord_table")
    void deleteAllRecords();

    @Query("SELECT * FROM newRecord_table ORDER BY NRdate DESC")
    LiveData<List<NewRecord>> getAllRecord();

    @Query("SELECT * FROM newRecord_table WHERE NRdate = (SELECT MAX(NRdate) FROM newRecord_table)")
    LiveData<List<NewRecord>> findRecordByDate ();
}  

我也有一个存储库类和视图模型类,但是我不在这里发布这些代码.
下面是我检索学生的姓名和手机号码并绑定到微调器中的代码.使用此代码,我想检索我从微调器中选择的学生的最新记录的页码.

I have a repository class and view model class also but I am not posting those codes here.
Below is the code where I retrieve student's name and mobile number and binding into a spinner. Using this code I want to retrieve page number of the latest record of the student that I select from the spinner.

newRecordViewModel = ViewModelProviders.of(AddRecord.this).get(NewRecordViewModel.class);
        newRecordViewModel.getAllRecords().observe(this, new Observer<List<NewRecord>>() {
            @Override
            public void onChanged(List<NewRecord> newRecords) {
            }
        });

        selectStudentName.setTitle(getResources().getString(R.string.select_student_name));
        selectStudentName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final AdapterView<?> adapterView, View view, final int position, final long l) {
                if ( position == -1 ) {
                    Toast.makeText(AddRecord.this, "No record is selected", Toast.LENGTH_SHORT).show();
                } else {
                    String name = adapterView.getItemAtPosition(position).toString();
                    Toast.makeText(AddRecord.this, name + " is selected", Toast.LENGTH_SHORT).show();
                    String studentMobile = Objects.requireNonNull(studentViewModel.getAllStudents().getValue()).get(position).getMobileNumber();
                    studentMOB.setText("Mobile: " + studentMobile);
                    recordIds = studentViewModel.getAllStudents().getValue().get(position).getId();

                }
            }  

我不清楚我是否足够清楚.我希望我是.请帮忙.谢谢

I don't know if I am clear enough. I hope I am. Please help. Thank you

推荐答案

您只需要创建一个新的Query即可获取所需的内容.

You just need to create a new Query to fetch what you need.

您要说的是要查找一个学生的记录并显示他阅读的最后一页,因此应该是这样

From what you say you want to find record from one student and display the last page he reads so it should be something like this

@Query("SELECT * FROM newRecord_table WHERE newRecord_table.studentid = :studentId ORDER BY newRecord_table.date, newRecord_table.page DESC ")
LiveData<List<NewRecord>> findLastPage(Integer studentId);

studentId是您要查找记录的userId.不要忘记处理一无所有的情况.

studentId is the userId you want to find the records. Don't forget to handle the case where there is nothing.

这篇关于Android Room数据库,检索输入的最新记录的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆