1

I'm new at Android and today I felt really motivated, so I started writing an app.

But now I'm stuck... I have an ArrayList with Strings in it. With a random number generator I get a String from this list. What I really want to do is avoiding repeating the Strings. I want to see an element only once.

Can you guys please help me?

Here is my MainActivity:

public class MainActivity extends AppCompatActivity {

private TextView welcomeText;
private TextView pokemons;
private TextView whatToDo;
private Button yesButton;
private Button noButton;

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

    final String[] pokemonok = {
            "Pikachu",
            "Charizard",
            "Bulbasaur",
            "Charmander",
            "Squirtle",
            "Caterpie",
            "Metapod",
            "Weedle",
            "Rattata",
            "Onix"
    };

    final List<String> list = new ArrayList<String>(pokemonok.length);
    for (String s : pokemonok) {
        list.add(s);
    }

    welcomeText = (TextView) findViewById(R.id.welcomeText);
    pokemons = (TextView) findViewById(R.id.pokemons);
    whatToDo = (TextView) findViewById(R.id.whatToDo);
    //EditText answerText = (EditText) findViewById(R.id.answerText);
    yesButton = (Button) findViewById(R.id.yesButton);
    noButton = (Button) findViewById(R.id.noButton);

    final String[] def = {"[ " + list.get(0) + " ]"};
    pokemons.setText(def[0]);

    View.OnClickListener igen = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Random r = new Random();
            int szam = r.nextInt(pokemonok.length);

            pokemons.setText(def[0] + "[ " + list.get(szam) + " ]");
            def[0] = def[0] + "[ " + list.get(szam) + " ]";

        }
    };

    yesButton.setOnClickListener(igen);

   }
}
gemboly
  • 15
  • 5

4 Answers4

2

If you want to remove elements from a list after accessing it, you can call

list.remove(i)

This will remove the ith element from the list.

sn2k
  • 64
  • 3
1

Depending on how you set up the random num generator, you could do something like this:

list.remove(randomNumber);

This should remove the item from the list.

OneSurvivor
  • 494
  • 4
  • 15
1

If you want to keep the list you can have another list, and whenever you read an object from the original list, add that element to the temporary list. Then, whenever you want to choose a new random element, first you have to check if that element is already on the temporary list or not. If yes, you have to regenerate the process.

List<String> tempList = new ArrayList<>();
tempList.add( // YOUR ELEMENT TO SAVE ON TEMP LIST // );

Then to check

if (tempList.contains(element)){
      // your code to doing the process again
{
Fred A
  • 116
  • 1
  • 6
0

If you don't want to change your pokemon String List then you could create an array of booleans in your class which identifies which pokemon has already been picked:

private boolean[] pokemonPicked = new boolean[pokemonok.length];

and you could change your clickListener method to this:

View.OnClickListener igen = new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        int r = new Random().nextInt(pokemonok.length);
        while(pokemonPicked[r]){
            r = new Random().nextInt(pokemonok.length);
        }
        pokemonPicked[r] = true;
        int szam = r;

        pokemons.setText(def[0] + "[ " + list.get(szam) + " ]");
        def[0] = def[0] + "[ " + list.get(szam) + " ]";

    }
};
  • This seems inefficient; you could have an arbitrary delay while you wait for a valid number to be picked. – D M Oct 11 '17 at 23:20
  • I wouldn't recommend it doing that too but in reality this works relatively fine because random numbers are equally distributed. In the worst case this could of course go wrong – Manh Khôi Duong Oct 11 '17 at 23:25
  • Well, for this relatively low numbers it's probably fine. If you were doing this for very large number of pokemon (like millions) it would eventually become a problem. – D M Oct 11 '17 at 23:30