UI不使用保存在SQLite数据库中的项目填充ListView

时间:2018-06-27 16:10:13

标签: android sqlite listview sharedpreferences android-cursorloader

我的MoviesApp遇到问题已有一段时间了,我觉得我已经用尽了所有的知识;我对Android很陌生,所以请耐心:-)

MoviesApp是一个简单的电影列表应用程序,用户可以在其中滚动浏览电影列表,查看每个电影的详细信息并将其收藏夹保存在SQLite数据库中。 我使用SharedPreference对影片的受欢迎程度,评分和收藏夹(数据库中保存的唯一列表)进行排序,但是当我对每个影片进行更改时,UI根本不会更新。

我真的很困,老实说,我可以用另一双眼睛做,因为即使答案盯着我,我也看不到

我将链接粘贴到下面的项目:

https://drive.google.com/file/d/1SweLpwfo5RntXrbtLPP3N_xS1bVs32Ze/view?usp=sharing

谢谢!

更新:我相信问题将在MainActivity类中,在该类中声明了RecyclerView Loader-特别是在onLoadFinished()中。

    @SuppressWarnings({"WeakerAccess", "unused", "CanBeFinal"})
    public class MainActivity extends AppCompatActivity implements
            LoaderManager.LoaderCallbacks,
            MovieAdapter.MovieDetailClickHandler, SwipeRefreshLayout.OnRefreshListener {

        private static final String TAG = MainActivity.class.getSimpleName();
        public static final String MOVIE_ID = "movieId";

        private final static String LIFECYCLE_CALLBACKS_LAYOUT_MANAGER_KEY = "KeyForLayoutManagerState";
        Parcelable savedLayoutManagerState;

        public RecyclerView movieListRV;
        private GridLayoutManager gridLayoutManager =
                new GridLayoutManager(this, 1);
        Context context = this;

        // Loader IDs for loading the main API and the poster API, respectively
        private static final int ID_LOADER_LIST_MOVIES = 1;
        private static final int ID_LOADER_CURSOR = 2;

        // adapter
        private MovieAdapter adapter;

        // detect internet connection
        NetworkDetection networkDetection;

        // swipe to refresh
        SwipeRefreshLayout swipeRefreshLayout;

        // sortOption
        String sortOption = null;

        // movie projection
        private final String[] projection = new String[]{
                MoviesContract.MovieEntry.COLUMN_MOVIE_POSTER,
                MoviesContract.MovieEntry.COLUMN_MOVIE_ID
        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Stetho.initializeWithDefaults(this);

            Toolbar toolbar = findViewById(R.id.settings_activity_toolbar);
            setSupportActionBar(toolbar);
            toolbar.setTitleTextColor(Color.WHITE);

            networkDetection = new NetworkDetection(this);

            swipeRefreshLayout = findViewById(R.id.discover_swipe_refresh);
            swipeRefreshLayout.setOnRefreshListener(MainActivity.this);
            swipeRefreshLayout.setColorScheme(android.R.color.holo_red_dark);

            movieListRV = findViewById(R.id.recycler_view_movies);
            movieListRV.setLayoutManager(gridLayoutManager);
            movieListRV.setHasFixedSize(true);

            ViewTreeObserver viewTreeObserver = movieListRV.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    calculateSize();
                }
            });
            adapter = new MovieAdapter(this, this);
            movieListRV.setAdapter(adapter);

            RecyclerViewItemDecorator itemDecorator = new RecyclerViewItemDecorator(context,
                    R.dimen.item_offset);
            movieListRV.addItemDecoration(itemDecorator);

            final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences
                    (context);
            SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener = new
                    SharedPreferences.OnSharedPreferenceChangeListener() {
                @Override
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                    adapter.deleteItemsInList();
                    onRefresh();
                    if (key.equals(getString(R.string.pref_sort_by_key))) {
                        initializeloader();
                    }
                }
            };
            preferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
            initializeloader();
        }

        private static final int sColumnWidth = 200;

        private void calculateSize() {
            int spanCount = (int) Math.floor(movieListRV.getWidth() / convertDPToPixels(sColumnWidth));
            ((GridLayoutManager) movieListRV.getLayoutManager()).setSpanCount(spanCount);
        }

        @SuppressWarnings("SameParameterValue")
        private float convertDPToPixels(int dp) {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            float logicalDensity = metrics.density;
            return dp * logicalDensity;
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putParcelable(LIFECYCLE_CALLBACKS_LAYOUT_MANAGER_KEY, gridLayoutManager
                    .onSaveInstanceState());
        }

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            if (savedInstanceState != null) {
                savedLayoutManagerState = savedInstanceState.getParcelable
                        (LIFECYCLE_CALLBACKS_LAYOUT_MANAGER_KEY);
                movieListRV.getLayoutManager().onRestoreInstanceState(savedLayoutManagerState);
            }
        }

        @Override
        public Loader onCreateLoader(int id, Bundle args) {
            adapter.deleteItemsInList();
            String urlMovieActivity;
            switch (id) {
                case ID_LOADER_CURSOR:
                    return new CursorLoader(context, MoviesContract.MovieEntry.MOVIES_CONTENT_URI,
                            projection, null, null, null);

                case ID_LOADER_LIST_MOVIES:
                    urlMovieActivity = NetworkUtils.buildUrlMovieActivity(context, sortOption);
                    return new MovieLoader(this, urlMovieActivity);
                default:
                    return null;
            }
        }

        @Override
        public void onLoadFinished(Loader loader, Object data) {
            adapter.deleteItemsInList();
            TextView noMoviesMessage = findViewById(R.id.no_movies_found_tv);
            switch (loader.getId()) {
                case ID_LOADER_CURSOR:
                    adapter.InsertList(data);
                    break;

                case ID_LOADER_LIST_MOVIES:
                    //noinspection unchecked
                    List<MovieItem> movieItems = (List<MovieItem>) data;
                    if (networkDetection.isConnected()) {
                        noMoviesMessage.setVisibility(View.GONE);
                        adapter.InsertList(movieItems);
                        movieListRV.getLayoutManager().onRestoreInstanceState(savedLayoutManagerState);
                    } else {
                        noMoviesMessage.setVisibility(View.VISIBLE);
                    }
                    break;
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onLoaderReset(Loader loader) {
            switch (loader.getId()) {
                case ID_LOADER_CURSOR:
                    adapter.InsertList(null);
                    break;
                case ID_LOADER_LIST_MOVIES:
                    adapter.InsertList(null);
                    break;
            }
        }

        @Override
        public void onPostResume(Loader loader) {
            super.onPostResume();
            getLoaderManager().initLoader(ID_LOADER_CURSOR, null, this);
        }

        @Override
        public void onSelectedItem(int movieId) {
            Intent goToDetailActivity = new Intent(this, DetailMovieActivity.class);
            goToDetailActivity.putExtra(MOVIE_ID, movieId);
            startActivity(goToDetailActivity);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_general, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            if (id == R.id.action_general_settings) {
                Intent goToSetting = new Intent(this, SettingsActivity.class);
                startActivity(goToSetting);
                return true;
            } else if (id == R.id.action_refresh) {
                onRefresh();
            }
            return super.onOptionsItemSelected(menuItem);
        }

        /**
         * Called when a swipe gesture triggers a refresh.
         */
        @Override
        public void onRefresh() {
            adapter.deleteItemsInList();
            swipeRefreshLayout.setRefreshing(false);

            restartloader();
            adapter.notifyDataSetChanged();
        }

        private void restartloader() {
            adapter.deleteItemsInList();
            if (MoviePreferences.getSortByPreference(context).equals(getString(R.string
                    .pref_sort_by_favourite))) {
                getLoaderManager().restartLoader(ID_LOADER_CURSOR, null, MainActivity
                        .this);
            }

            if (MoviePreferences.getSortByPreference(context).equals(getString(R.string
                    .pref_sort_by_popularity))) {
                sortOption = NetworkUtils.MOST_POPULAR_PARAM;
                getLoaderManager().restartLoader(ID_LOADER_LIST_MOVIES, null,
                        MainActivity.this);
            }

            if (MoviePreferences.getSortByPreference(context).equals(getString(R.string
                    .pref_sort_by_rating))) {
                sortOption = NetworkUtils.TOP_RATED_PARAM;
                getLoaderManager().restartLoader(ID_LOADER_LIST_MOVIES, null,
                        MainActivity.this);
            }
            adapter.notifyDataSetChanged();
        }

        public void initializeloader() {
            restartloader();
            if (MoviePreferences.getSortByPreference(context).equals(getString(R.string
                    .pref_sort_by_favourite))) {
                getLoaderManager().initLoader(ID_LOADER_CURSOR, null, MainActivity
                        .this);
            }

            if (MoviePreferences.getSortByPreference(context).equals(getString(R.string
                    .pref_sort_by_popularity))) {
                onRefresh();

                sortOption = NetworkUtils.MOST_POPULAR_PARAM;
                getLoaderManager().initLoader(ID_LOADER_LIST_MOVIES, null,
                        MainActivity.this);
            }

            if (MoviePreferences.getSortByPreference(context).equals(getString(R.string
                    .pref_sort_by_rating))) {
                onRefresh();

                sortOption = NetworkUtils.TOP_RATED_PARAM;
                getLoaderManager().initLoader(ID_LOADER_LIST_MOVIES, null,
                        MainActivity.this);
            }

            adapter.notifyDataSetChanged();
        }
    } 

0 个答案:

没有答案