Advertisement
Kosheen

Activity

Nov 8th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. package com.alexandra.movies;
  2.  
  3. import android.content.res.Resources;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.TextView;
  8.  
  9. import java.util.Random;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.     TextView resultMovieView;
  13.  
  14.     Resources res;
  15.     String[] movies;
  16.     String[] shuffledMovies;
  17.     int movieIndex;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         resultMovieView = findViewById(R.id.result);
  25.  
  26.         res = getResources();
  27.         movies = res.getStringArray(R.array.movies);
  28.         shuffledMovies = shuffleMovies(movies);
  29.         movieIndex = 0;
  30.     }
  31.  
  32.     public void beginMovies(View v) {
  33.         shuffledMovies = shuffleMovies(movies);
  34.         movieIndex = 0;
  35.         resultMovieView.setText("");
  36.     }
  37.  
  38.     public void showMovie(View v) {
  39.         if (movieIndex >= shuffledMovies.length) {
  40.             resultMovieView.setText("Фильмы закончились!");
  41.             return;
  42.         }
  43.         String movie = shuffledMovies[movieIndex++];
  44.         resultMovieView.setText(movie);
  45.     }
  46.  
  47.     public String[] shuffleMovies(String[] movies) {
  48.         String tempMovie;
  49.  
  50.         Random rnd = new Random(System.currentTimeMillis());
  51.         int randIndex;
  52.  
  53.         for (int i = 0; i < movies.length; i++) {
  54.             randIndex = rnd.nextInt(movies.length - 1);
  55.  
  56.             tempMovie = movies[0];
  57.             movies[0] = movies[randIndex];
  58.             movies[randIndex]= tempMovie;
  59.         }
  60.         return movies;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement