Konstantinos Katsis Konstantinos Katsis - 1 month ago 7
Android Question

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 and a random generator that display a country to a textView for the user.If the user press yes button then the generator runs.If the user press no button nothing happens so far but the idea is to delete the element of the array that is displaying and generator will run again but with this element deleted.`

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



yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] country = new String[10];
country[0] = "greece";
country[1] = "germany";
country[2] = "america";
country[3] = "serbia";
country[4] = "france";
country[5] = "england";
country[6] = "cyprus";
country[7] = "japan";
country[8] = "russia";
country[9] = "china";
Random rand = new Random();
int randome = rand.nextInt(10);
tvMessage.setText(country[randome]);

}
});




}

Answer

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