在Android中完成Java类方法时设置进度栏

时间:2018-09-02 15:47:49

标签: java android class

我正在使用非活动类填充Framelayout。我想在加载Framelayout时显示进度栏,并在它是Framelayout时隐藏 加载完成。

我有活动类的经验,但无法显示或隐藏非活动类的进度条

我在启动应用程序时首先调用此方法

 private static void getMovies(final Activity activity, final String type, final MoviesCallback callback) 

这是我的班级代码

public class MoviesUtil {
    private static final Webb WEBB = Webb.create();

    private static final String TMDB_API_MOVIES_URL = "http://api.themoviedb.org/3/movie/%s?api_key=%s&page=%s";
    private static final String TMDB_API_VIDEOS_URL = "http://api.themoviedb.org/3/movie/%s/videos?api_key=%s";
    private static final String TMDB_API_REVIEWS_URL = "http://api.themoviedb.org/3/movie/%s/reviews?api_key=%s";
    private static final String TMDB_POSTER_URL = "https://image.tmdb.org/t/p/w185%s";
    private static final String TMDB_BACKDROP_URL = "https://image.tmdb.org/t/p/w300%s";

    private static final String TYPE_POPULAR = "popular";
    private static final String TYPE_TOP_RATED = "top_rated";
    private static final String TYPE_FAVORITES = "favorites";
    private static ProgressDialog progressDialog;

    public static boolean isFavorite(Context context, Movie movie) {
        Cursor cursor = context.getContentResolver()
                .query(MovieContract.CONTENT_URI,
                        null,
                        String.format("%s = ? and %s = ?", MovieContract.MOVIE_ID, MovieContract.TYPE),
                        new String[]{movie.getId() + "", TYPE_FAVORITES},
                        null
                );
        boolean isFavorite = cursor.getCount() > 0;
        cursor.close();
        return isFavorite;
    }



    public static boolean toggleFavorite(Context context, Movie movie) {
        if (isFavorite(context, movie)) {
            deleteMovie(context, TYPE_FAVORITES, movie);
            return false;
        } else {
            saveMovie(context, TYPE_FAVORITES, movie);
            return true;
        }
    }



    public static void getPopularMovies(Activity activity, MoviesCallback callback) {
        getMovies(activity, TYPE_POPULAR, callback);

    }

    public static void getTopRatedMovies(Activity activity, MoviesCallback callback) {
        getMovies(activity, TYPE_TOP_RATED, callback);
    }

    public static void getFavoritesMovies(Activity activity, MoviesCallback callback) {
        getMovies(activity, TYPE_FAVORITES, callback);
    }

    private static void getMovies(final Activity activity, final String type, final MoviesCallback callback) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                if (Util.isConnected(activity, false) && !type.equals(TYPE_FAVORITES)) {
                    getMoviesFromApi(activity, type);
                }
                getMoviesFromDb(activity, type, callback);


                progressDialog = new ProgressDialog(activity);
                progressDialog.setMax(100);
                progressDialog.setMessage("Its loading....");
                progressDialog.setTitle("ProgressDialog bar example");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.show();



            }
        });
    }

    private static void getMoviesFromApi(Activity activity, String type) {
        String apiUrl = String.format(TMDB_API_MOVIES_URL, type, activity.getString(R.string.tmdb_api_key), 1);
        try {
            JSONArray moviesJson = WEBB.get(apiUrl)
                    .asJsonObject()
                    .getBody()
                    .getJSONArray("results");

            List<Movie> movies = toMovies(activity, moviesJson);
            deleteMovies(activity, type);
            saveMovies(activity, type, movies);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }



    private static void getMoviesFromDb(Activity activity, String type, final MoviesCallback callback) {
        try {
            Cursor cursor = activity.getContentResolver()
                    .query(MovieContract.CONTENT_URI,
                            null,
                            MovieContract.TYPE + " = ?",
                            new String[]{type},
                            null

                    );
            final List<Movie> movies = toMovies(cursor);
            cursor.close();
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.success(movies);
                    progressDialog.dismiss();

                }
            });
        } catch (final Exception e) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.error(e);
                }
            });

        }
    }

    public static void getReviewsFromApi(final Activity activity, final Movie movie, final ReviewsCallback callback) {
        if (Util.isConnected(activity, false)) {
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    String apiUrl = String.format(TMDB_API_REVIEWS_URL, movie.getId(), activity.getString(R.string.tmdb_api_key));
                    final List<Review> reviews = new ArrayList<>();
                    try {
                        JSONArray reviewsJson = WEBB.get(apiUrl)
                                .asJsonObject()
                                .getBody()
                                .getJSONArray("results");
                        reviews.addAll(toReviews(reviewsJson));
                    } catch (final Exception e) {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                callback.error(e);
                            }
                        });
                    }
                    if (reviews.isEmpty()) {
                        Review review = new Review();
                        review.setContent(activity.getString(R.string.no_review_found));
                        reviews.add(review);
                    }
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callback.success(reviews);
                        }
                    });
                }
            });
        } else {
            Review review = new Review();
            review.setContent(activity.getString(R.string.conn_internet));
            final List<Review> reviews = new ArrayList<>();
            reviews.add(review);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.success(reviews);
                }
            });
        }
    }

    private static void saveMovie(final Context context, final String type, final Movie movie) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                List<Movie> movies = new ArrayList<>();
                movies.add(movie);
                saveMovies(context, type, movies);
            }
        });
    }

    private static void saveMovies(Context context, String type, List<Movie> movies) {
        if (movies != null) {
            ContentValues[] moviesValues = new ContentValues[movies.size()];
            for (int i = 0; i < movies.size(); i++) {
                try {
                    Movie movie = movies.get(i);
                    ContentValues movieValues = new ContentValues();
                    movieValues.put(MovieContract.MOVIE_ID, movie.getId());
                    movieValues.put(MovieContract.TYPE, type);
                    movieValues.put(MovieContract.TITLE, movie.getTitle());
                    movieValues.put(MovieContract.OVERVIEW, movie.getOverview());
                    movieValues.put(MovieContract.POSTER_URL, movie.getPosterUrl());
                    movieValues.put(MovieContract.BACKDROP_URL, movie.getBackdropUrl());
                    movieValues.put(MovieContract.TRAILER_URL, movie.getTrailerUrl());
                    movieValues.put(MovieContract.RELEASE_DATE, Util.toDbDate(movie.getReleaseDate()));
                    movieValues.put(MovieContract.RATING, movie.getRating());
                    movieValues.put(MovieContract.ADULT, movie.isAdult() ? 1 : 0);
                    moviesValues[i] = movieValues;
                } catch (Exception ignore) {
                }
            }
            context.getContentResolver()
                    .bulkInsert(MovieContract.CONTENT_URI, moviesValues);
        }
    }

    private static void deleteMovie(final Context context, final String type, final Movie movie) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                context.getContentResolver()
                        .delete(MovieContract.CONTENT_URI,
                                MovieContract.MOVIE_ID + " = ? and " + MovieContract.TYPE + " = ?",
                                new String[]{movie.getId() + "", type});
            }
        });
    }

    private static void deleteMovies(final Context context, final String type) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                context.getContentResolver()
                        .delete(MovieContract.CONTENT_URI,
                                MovieContract.TYPE + " = ?",
                                new String[]{type});
            }
        });
    }

    private static List<Movie> toMovies(Cursor cursor) {
        List<Movie> movies = new ArrayList<>();
        while (cursor.moveToNext()) {
            Movie movie = new Movie();
            movie.setId(cursor.getInt(
                    cursor.getColumnIndex(MovieContract.MOVIE_ID)));
            movie.setTitle(cursor.getString(
                    cursor.getColumnIndex(MovieContract.TITLE)));
            movie.setOverview(cursor.getString(
                    cursor.getColumnIndex(MovieContract.OVERVIEW)));
            movie.setPosterUrl(cursor.getString(
                    cursor.getColumnIndex(MovieContract.POSTER_URL)));
            movie.setBackdropUrl(cursor.getString(
                    cursor.getColumnIndex(MovieContract.BACKDROP_URL)));
            movie.setTrailerUrl(cursor.getString(
                    cursor.getColumnIndex(MovieContract.TRAILER_URL)));
            movie.setReleaseDate(Util.toDate(cursor.getString(
                    cursor.getColumnIndex(MovieContract.RELEASE_DATE))));
            movie.setRating(cursor.getFloat(
                    cursor.getColumnIndex(MovieContract.RATING)));
            movie.setAdult(cursor.getInt(
                    cursor.getColumnIndex(MovieContract.ADULT)) == 1);
            movies.add(movie);
        }
        return movies;
    }

    private static List<Movie> toMovies(Context context, JSONArray jsonMovies) {
        List<Movie> movies = new ArrayList<>();
        if (jsonMovies != null) {
            for (int i = 0; i < jsonMovies.length(); i++) {
                try {
                    JSONObject jsonMovie = jsonMovies.getJSONObject(i);
                    int movieId = jsonMovie.getInt("id");
                    Movie movie = new Movie();
                    movie.setId(movieId);
                    movie.setTitle(jsonMovie.getString("title"));
                    movie.setOverview(jsonMovie.getString("overview"));
                    movie.setPosterUrl(String.format(TMDB_POSTER_URL, jsonMovie.getString("poster_path")));
                    movie.setBackdropUrl(String.format(TMDB_BACKDROP_URL, jsonMovie.getString("backdrop_path")));
                    movie.setTrailerUrl(getTrailerUrl(context, movieId));
                    movie.setReleaseDate(Util.toDate(jsonMovie.getString("release_date")));
                    movie.setRating((float) jsonMovie.getDouble("vote_average"));
                    movie.setAdult(jsonMovie.getBoolean("adult"));
                    movies.add(movie);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return movies;
    }

    private static List<Review> toReviews(JSONArray jsonReviews) {
        List<Review> reviews = new ArrayList<>();
        if (jsonReviews != null) {
            for (int i = 0; i < jsonReviews.length(); i++) {
                try {
                    JSONObject jsonReview = jsonReviews.getJSONObject(i);
                    Review review = new Review();
                    review.setAuthor(jsonReview.getString("author"));
                    review.setContent(jsonReview.getString("content"));
                    reviews.add(review);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return reviews;
    }

    private static String getTrailerUrl(Context context, int movieId) {
        String apiUrl = String.format(TMDB_API_VIDEOS_URL, movieId, context.getString(R.string.tmdb_api_key));
        try {
            JSONArray trailersJson = WEBB.get(apiUrl)
                    .asJsonObject()
                    .getBody()
                    .getJSONArray("results");
            for (int i = 0; i < trailersJson.length(); i++) {
                JSONObject trailerJson = trailersJson.getJSONObject(i);
                if (trailerJson.getString("site").toLowerCase().equals("youtube")) {
                    return "https://youtube.com/watch?v=" + trailerJson.getString("key");
                }
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}

我该怎么做。

我们将不胜感激,感谢您。

我的日志猫错误是

09-05 20:10:57.639 2183-2891/? E/GpsXtraDownloader: No XTRA servers were specified in the GPS configuration
09-05 20:10:58.319 9101-9101/com.nepalpolice.cinemaghar E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
09-05 20:10:58.359 9101-9101/com.nepalpolice.cinemaghar E/Iconics: Wrong icon name: 2131165281
09-05 20:10:58.600 9101-9101/com.nepalpolice.cinemaghar E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
    No adapter attached; skipping layout
09-05 20:10:58.790 9101-9101/com.nepalpolice.cinemaghar E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
    No adapter attached; skipping layout
09-05 20:11:06.758 9101-9101/com.nepalpolice.cinemaghar E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nepalpolice.cinemaghar, PID: 9101
    java.lang.NullPointerException
        at com.nepalpolice.cinemaghar.util.MoviesUtil$2.run(MoviesUtil.java:138)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5018)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

解决方案:

代替:

ProgressBar progressBar;

写:

ProgressDialog progressDialog;

修改:

private static void getMovies(final Activity activity, final String type, final MoviesCallback callback) {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            if (Util.isConnected(activity, false) && !type.equals(TYPE_FAVORITES)) {
                getMoviesFromApi(activity, type);
            }
            getMoviesFromDb(activity, type, callback);

                progressDialog = new ProgressDialog(activity);
                progressDialog.setMax(100);
                progressDialog.setMessage("Its loading....");
                progressDialog.setTitle("ProgressDialog bar example");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.show();

        }
    });
}

然后在这里添加:

  final List<Movie> movies = toMovies(cursor);
        cursor.close();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callback.success(movies);
                .........(Write Here)
            }
        });

此:

progressDialog.dismiss();

让我知道它是否有效。