Konstantinos Katsis Konstantinos Katsis - 1 month ago 11
Android Question

Studio Change Array Value Part 2

On part 1 I asked how to delete an element of my array if the user selects an action instead of another. I got an answer about making my string array an arraylist.

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.

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 of my previous question with the arraylist. If there is no way to do it with arraylist then I should do it with string array like this, 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";

Answer

if the user press yes to "Do you wanna see a Comedy Movie" i want somehow to display a movie that is stored on another array or something like that

You can check what the textview text equals when you click the button, then get the appropriate data out of some list/array.

final Random rand = new Random();
final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);

// Some prompts to compare against later
final String actionMovie = "Do you wanna see an Action Movie???";
final ArrayList<String> prompts = new ArrayList<>();
prompts.add(actionMovie);

// Your lists
final ArrayList<String> actionMovies = new ArrayList<>();
actionMovies.add("Terminator 2");

// Setup the click event
findViewById(R.id.Yes).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        // Check which prompt is shown
        if (tvMessage.getText().toString().equals(actionMovie)) {

            // Get a random movie
            String movieTitle = actionMovies.get(rand.nextInt(actionMovies.size()));

        } else { } // equals something else
    }