lundi 24 octobre 2016

Android Studio Change Array Value Part 2

On part 1 i did ask how to delete an element of my array if the user selects an action instead of another.I did got an answer about making my string array an array list.Now i 'd like to know how to give id values to my data in order to display the next children on that value.For example i would like to display the cities of greece if the user press yes to greece.This is a general question i have and i am writing a small sample of code to give you understand what i mean.

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("Do you wanna see an Action Movie???");
    country.add("Do you wanna see a Comedy Movie???");
    country.add("Do you wanna see a Drama Movie???");
    no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (randome>=0 && randome<country.size()){
                country.remove(randome);
                Random rand = new Random();
                randome = rand.nextInt(country.size());
                tvMessage.setText(country.get(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));


        }
    });
}

That's what i used for the first part "Android Studio Change Array Value" with the array list.If there is now way to do it with array list then i should do it with string array like that i guess

String[][] countriesAndCities= new String[][];
countriesAndCities [0][0] = "United Kingdom";
countriesAndCities [0][1] = "London";
countriesAndCities [1][0] = "USA";
countriesAndCities [1][1] = "Washington";
countriesAndCities [2][0] = "India";
countriesAndCities [2][1] = "New Delhi";




Aucun commentaire:

Enregistrer un commentaire