【问题标题】:Android Room selecting a row with queryAndroid Room 选择带有查询的行
【发布时间】:2018-10-04 17:20:58
【问题描述】:

我是使用 Room 功能的新手,我已经使用以下设置了我的 Dao

公共接口 ValleyDao {

@Query("SELECT * FROM event_filter ORDER BY filter_name ASC")
LiveData<List<EventFilter>> getAllEventsFilters();

@Query("SELECT * FROM event_filter LIMIT 1")
EventFilter[] getAnyEventFilter();

@Query("SELECT * FROM events ORDER BY event_millis ASC")
LiveData<List<Events>> getAllEvents();

@Query("SELECT * FROM events WHERE event_filter = :event_filter")
LiveData<List<Events>> getFilteredEvents(List<Events> event_filter);

@Query("SELECT * FROM events LIMIT 1")
Events[] getAnyEvent();

@Insert(onConflict = OnConflictStrategy.IGNORE)
void insertNewEvents(Events events);

@Insert(onConflict = OnConflictStrategy.IGNORE)
void insertNewEventFilters(EventFilter eventFilter);

@Delete
void deleteEvent(Events events);

@Query("DELETE FROM events")
void deleteAllEvents();

@Update
void update(Events... events);

}

给我问题的查询是查询:

@Query("SELECT * FROM events WHERE event_filter = :event_filter") LiveData> getFilteredEvents(List event_filter);

我在列中分配了一个数字过滤器,因此我可以对数据进行排序。我正在尝试获取该整数,以便可以在回收站视图中显示正确的项目。我不确定在 VeiwModeler 中放什么来限制它。到目前为止,我的 ViewModeler 如下所示:

public class ValleyViewModel extends AndroidViewModel {

private DataRepository mRepository;

private LiveData<List<Events>> mAllEvents;

private LiveData<List<EventFilter>> mAllEventsFilter;

public ValleyViewModel(@NonNull Application application) {
    super(application);
    mRepository = new DataRepository(application);
    mAllEvents = mRepository.getmAllEvents();
    mAllEventsFilter = mRepository.getmAllEventFilters();
}

LiveData<List<Events>> getAllEvents() {
    return mAllEvents;
}

LiveData<List<EventFilter>> getmAllEventsFilter() {return mAllEventsFilter;}



public void insert(Events events) {
    mRepository.insert(events);
}

public void deleteAll() {
    mRepository.deleteAll();
}

public void deleteWord(Events events) {
    mRepository.deleteWord(events);
}

public void update(Events events) {
    mRepository.update(events);
}

}

如何在 ROOM 中使用 WHERE 条件?

【问题讨论】:

    标签: java database android-room


    【解决方案1】:

    这是我最终为解决方案所做的:

    道中:

    @Query("SELECT * FROM events WHERE event_filter = :event_id")
    LiveData<List<Events>> findSpecificEvent(long event_id);
    

    LiveData 可以到达我不在主 UI 线程上运行的位置。

    在 AppDatabase(Room 结构中的抽象类)中无需更改。

    在存储库中:(它有效,但我不确定这是否是最好的方法)

    public LiveData<List<Events>> getmCertainEvents(long eventsId) {
                return mDao.findSpecificEvent(eventsId);
        }
    

    在 ViewModeler 中:

    LiveData<List<Events>> getmCertainEvents(long eventId) {
          return mRepository.getmCertainEvents(eventId);
    }   
    

    最后在我的列表活动中:

    final EventRecyclerAdapter adapter = new EventRecyclerAdapter(this);
    
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
                // Set up the WordViewModel.
                mViewModel = ViewModelProviders.of(this).get(ValleyViewModel.class);
       mViewModel.getmCertainEvents(FILTERLEVEL).observe(this, new Observer<List<Events>>() {
                        @Override
                        public void onChanged(@Nullable final List<Events> events) {
                            // Update the cached copy of the words in the adapter.
                            adapter.setEvents(events);
    
                        }
                    });
    

    【讨论】:

      【解决方案2】:

      “我不知道在 VeiwModeler 中放什么来限制它”,如果理解正确,你想限制查询。

      根据我的理解,这是在 Dao 类中完成的,例如,在 ValleyDao 中您将运行 SQLite 查询:

      @Query("SELECT * FROM events WHERE id = :event_id")
      Event findSpecificEvent(long event_id);
      

      在上面的代码示例中,我试图找到id1 的特定事件。然后在Repository:

      public Event findSpecificEvent(long event_id) {
          return mValleyDao.findSpecificEvent(event_id);
      }
      

      ViewModel类中:

      public Event findSpecificEvent(long event_id) {
          return mRepository.findSpecificEvent(event_id);
      }
      

      在我想将Event 传递给recyclerViewadapter 的调用类中:

      Event event = mViewModel.findSpecificEvent(1);
      // An example to pass it to recyclerVeiw
      recyclerViewAdapter.setEvent(event);
      

      请记住,在使用上述示例时,您需要在后台线程上执行此操作。

      【讨论】:

        猜你喜欢
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 2022-07-07
        • 2019-02-01
        • 1970-01-01
        • 2020-05-07
        • 2017-08-14
        相关资源
        最近更新 更多