Images are repeating in Imageviews

Asked

Viewed 153 times

4

I have 54 images need to show only 8, but have to have a random selection and can not repeat, I am programming on the platform of Android Studio.

I use this code it works smoothly, shows the results in 8 ImagemView ,More.... it repeats the results of the image ;(

insira o código aqui


public void Button2(View view) {

        final int[] imageViews = {
                R.id.imageView, R.id.imageView4, R.id.imageView7,
                R.id.imageView2, R.id.imageView5, R.id.imageView8,
                R.id.imageView3, R.id.imageView6,};


        final int[] images = {

                R.drawable.1,
                R.drawable.2,
                R.drawable.3,
                R.drawable.4,
                ...... vai até o 54


        };


        final Button shuffle = (Button) findViewById(R.id.Button2);
        shuffle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Random generator = new Random();
                int n = 9;
                n = generator.nextInt(n);
                Random random = new Random(System.currentTimeMillis());
                for (int v : imageViews) {
                    ImageView iv = (ImageView) findViewById(v);
                    iv.setImageResource(images[generator.nextInt(images.length - 1)]);
                }
            }
        });
    }

insira o código aqui

I used this Code below solved the problem.

};

final Button shuffle = (Button) findViewById(R.id.Button4);
shuffle.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {

        Random rng = new Random();
        List<Integer> generated = new ArrayList<Integer>();
        for (int i = 0; i < 8; i++)
        {
            while(true)
            {
                Integer next = rng.nextInt(54) ;
                if (!generated.contains(next))
                {
                    generated.add(next);
                    ImageView iv = (ImageView)findViewById(imageViews[i]);
                    iv.setImageResource(images[next]);
                    break;
                }
            }
        }
    }
});}}
insira o código aqui

Source:https://stackoverflow.com/questions/7470509/how-to-set-random-images-to-imageviews

  • Wallace this site works differently than a forum, so you don’t need to add that it was solved in the title. instead, you can accept an answer as accepted by clicking on v next to the answer. Thus, it is as if the question was marked as "solved".

  • Funny, you answered your question in your own question, which is perhaps very similar to my answer.

1 answer

2


You can do so by comparing whether the generated number already exists in the ArrayList:

int totalImagens = 54;
int totalEscolher = 10;
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random randomGenerator = new Random();
while(numbers.size() < totalEscolher)
    {
        int random = randomGenerator.nextInt(totalImagens);
        if (!numbers.contains(random)) {
            numbers.add(random);
        }
}

Thinking this way, yours setOnClickListener would look like this:

final Button shuffle = (Button) findViewById(R.id.Button2);
        shuffle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                int totalImagens = images.length;
                int totalEscolher = 10;
                final ArrayList<Integer> numbers = new ArrayList<Integer>();
                Random randomGenerator = new Random();
                while(numbers.size() < totalEscolher)
                {
                    int random = randomGenerator.nextInt(totalImagens);
                    if (!numbers.contains(random)) {
                        numbers.add(random);
                    }
                }

                for (int v : numbers) {
                    imageViews[v] = images[numbers.get(v)];
                    ImageView iv = (ImageView) findViewById(v);
                    iv.setImageResource(imageViews[v]);
                }
            }
        });
  • Okay, thanks for the help, but as I can integrate this code into my project, sorry I don’t mean much. I await response, meanwhile I will try to implement it here.

  • @Wallaceroberto edited the answer. Try doing it this way to see if it works.

  • Ta, and as I do now at the top, I’m searching here about Arraylist more unsuccessfully, if you see up there I’m using Images (R.drawable.example) and the second line ("final int[] imageViews = {") it continues?

  • @Wallaceroberto blz! I edited again for you. Now taking the image that is in your images. On top you can leave the same way by inserting the images.

  • You’re making a mistake right after you hit the button. 09-14 17:47:22.890 343-425/system_process W/Networkmanagementsockettagger: setKernelCountSet(10054, 0) failed with Errno -1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.