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 = new ArrayList<String>();
customlist.addAll(country);
customlist.remove(randome);
String []copycountry = new String[customlist.size()];
customlist.toArray(copycountry);
Solution 2:
It's better to use List when array size is not fixed.
int randome=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button no = (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 = new ArrayList<>();
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(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (randome>=0 && randome<country.size()){
country.remove(randome);
}
}
});
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random rand = new Random();
randome = rand.nextInt(country.size());
tvMessage.setText(country.get(randome));
}
});
}
Post a Comment for "Android Studio Change Array Value"