Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alexandra.movies;
- import android.content.res.Resources;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- TextView resultMovieView;
- Resources res;
- String[] movies;
- String[] shuffledMovies;
- int movieIndex;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- resultMovieView = findViewById(R.id.result);
- res = getResources();
- movies = res.getStringArray(R.array.movies);
- shuffledMovies = shuffleMovies(movies);
- movieIndex = 0;
- }
- public void beginMovies(View v) {
- shuffledMovies = shuffleMovies(movies);
- movieIndex = 0;
- resultMovieView.setText("");
- }
- public void showMovie(View v) {
- if (movieIndex >= shuffledMovies.length) {
- resultMovieView.setText("Фильмы закончились!");
- return;
- }
- String movie = shuffledMovies[movieIndex++];
- resultMovieView.setText(movie);
- }
- public String[] shuffleMovies(String[] movies) {
- String tempMovie;
- Random rnd = new Random(System.currentTimeMillis());
- int randIndex;
- for (int i = 0; i < movies.length; i++) {
- randIndex = rnd.nextInt(movies.length - 1);
- tempMovie = movies[0];
- movies[0] = movies[randIndex];
- movies[randIndex]= tempMovie;
- }
- return movies;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement