Android Studio Change Array Value
Hi i would like to know if there is a method that takes an element of my array and deleting it depending the answer of the user.For example in my code i have an array of countries
Solution 1:
Use ArrayList to delete dynamic index value
ArrayList<String> customlist = newArrayList<String>();
customlist.addAll(country);
customlist.remove(randome);
String []copycountry = newString[customlist.size()];
customlist.toArray(copycountry);
Solution 2:
It's better to use List when array size is not fixed.
int randome=0;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finalButtonno= (Button) findViewById(R.id.No);
final Button yes=(Button) findViewById(R.id.Yes);
final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);
final ArrayList<String> country = newArrayList<>();
country.add("greece");
country.add("germany");
country.add("america");
country.add("serbia");
country.add("france");
country.add("england");
country.add("cyprus");
country.add("japan");
country.add("russia");
country.add("china");
no.setsetOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
if (randome>=0 && randome<country.size()){
country.remove(randome);
}
}
});
yes.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
Randomrand=newRandom();
randome = rand.nextInt(country.size());
tvMessage.setText(country.get(randome));
}
});
}
Post a Comment for "Android Studio Change Array Value"