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);
}
}