Add a cutom class to an Android project by going to File menu -> New -> Java Class
Usage:
Custom class…
package eu.kio.android.beeradvier; import java.util.ArrayList; import java.util.List; public class BeerExpert { List<String> getBrands(String color){ List<String> brands = new ArrayList<>(); if(color.equals("amber")){ brands.add("Jack Amber"); brands.add("Red Moose"); } else{ brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return brands; } }
Main activity
package eu.kio.android.beeradvier; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Spinner; import java.util.List; public class FindBeerActivity extends Activity { private BeerExpert expert = new BeerExpert(); //called when the user clicks the button public void onClickFindBeer(View view){ //Get a reference to the TextView TextView brands = (TextView)findViewById(R.id.textView); //Get a reference to the Spinner Spinner color = (Spinner)findViewById(R.id.color); //Get the selected item in the Spinner String beerType = String.valueOf(color.getSelectedItem()); List<String> brandList = expert.getBrands(beerType); StringBuilder brandsFormatted = new StringBuilder(); for(String brand : brandList){ brandsFormatted.append(brand).append('\n'); } //Display the selected item brands.setText(brandsFormatted); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_beer); } }