Recyclerview不更新ROOM sqlite数据

时间:2019-06-24 14:22:07

标签: android-recyclerview android-sqlite android-room

我正在使用ROOM数据库以便从数据库中添加和获取数据,并使用RecyclerView显示数据。这里的问题是某些更新回收站视图未在回收站视图中显示更新的数据之后,但是我的数据库正确地更新了每个查询。 另一个问题是在更新数据时我无法使用加载。

我每次都关闭数据库,我认为RecyclerView在更新数据库时没有等待。

我在信息中收到此消息 由于数据库忙,无法更改数据库日志。进行中... 由于数据库忙,无法更改数据库日志。进行中.. 只是为了告知我我在同一方法中使用多个查询

我需要知道是否有什么办法可以保留数据库直到查询完全执行,然后可以更新数据库,这也将帮助我在执行查询时向用户显示“正在加载”

private void AllStationsList() {
    if (FinalList != null) {
        FinalList.clear();
    }
    getOnlyFavourites();
    getAllWithoutFavourites();
}

private void getOnlyFavourites() {
    //Get stationList of only favourites
    StationListRepository stationListRepositoryOnlyFavourites = new StationListRepository(getApplicationContext());
    stationListRepositoryOnlyFavourites.getStationListFavourites().observe(this, new Observer<List<StationList>>() {
        @Override
        public void onChanged(List<StationList> OnlyFavouritesstations) {
            assert OnlyFavouritesstations != null;

            if (OnlyFavouritesstations.size() > 0) {
                OnlyFavouritesstations = sortList(OnlyFavouritesstations);
                FinalList = OnlyFavouritesstations;
            }
        }
    });
    stationListRepositoryOnlyFavourites.closeDB();

}

private void getAllWithoutFavourites() {
    //Get stationList of all stations without favourites
    StationListRepository stationListwithoutFavorites = new StationListRepository(getApplicationContext());
    stationListwithoutFavorites.getStationList().observe(this, new Observer<List<StationList>>() {

        @Override
        public void onChanged(List<StationList> stationsWithoutFavouries) {
            Log.d("NotfavList", "" + stationsWithoutFavouries.size());
            assert stationsWithoutFavouries != null;
            if (stationsWithoutFavouries.size() > 0 || FinalList.size() > 0) {

                if (FinalList != null && FinalList.size() > 0) {
                    stationsWithoutFavouries = sortList(stationsWithoutFavouries);
                    FinalList.addAll(stationsWithoutFavouries);

                } else {
                    stationsWithoutFavouries = sortList(stationsWithoutFavouries);
                    FinalList = stationsWithoutFavouries;
                }
                Log.d("FinalListSizeActiveALL", "" + FinalList.size());
                detailsAdapter = new db_stationListAdapter(context, FinalList);
                recyclerView.setAdapter(detailsAdapter);
                /*detailsAdapter.notifyDataSetChanged();*/
                for (stationPostion = 0; stationPostion < FinalList.size(); stationPostion++) {
                    if (FinalList.get(stationPostion).getStationId().equalsIgnoreCase(Common.SELECTED_STATION) && onStationClick == true) {
                        Common.SCROLL_POSITION = stationPostion;
                        onStationClick = false;
                        break;
                    } else {
                        Common.SCROLL_POSITION = 0;
                    }
                }

                recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                    @Override
                    public void onLayoutChange(View v, int left, int top, int
                            right, int bottom, int oldLeft, int oldTop, int oldRight,
                                               int oldBottom) {


                        recyclerView.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                onStationClick = false;
                                recyclerView.smoothScrollToPosition(Common.SCROLL_POSITION);

                            }
                        }, 500);
                    }

                });
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //stopLoading();
                        stopAnimation();
                    }
                });
            }
        }
    });
}

0 个答案:

没有答案