I have an activity with two spinners. One is the type of hair and the other is the color of the hair. I want for a picture (on a head) to change based on the two items selected selected from the spinners.
Here's what it should do:
But step 3 doesn't work!!!
Heres my code:
public class HeadZoom extends MainActivity implements AdapterView.OnItemSelectedListener{
private Spinner hairSpinner;
private Spinner hairColor;
private String type;
private String color;
private ImageView imageView;
private List<String> hairTypeArray = new ArrayList<String>();
private List<String> hairColorArray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.head_zoom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView = (ImageView) findViewById(R.id.head);
type="Bald";
hairSpinner = (Spinner) findViewById(R.id.hairSpinner);
hairSpinner.setOnItemSelectedListener(this);
List<String> hairTypeArray = new ArrayList<String>();
hairTypeArray.add("Bald");
hairTypeArray.add("Female Short");
hairTypeArray.add("Female Medium");
(...)
ArrayAdapter<String> hairTypeAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairTypeArray);
hairTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairSpinner.setAdapter(hairTypeAdapter);
color = "Black";
hairColor = (Spinner) findViewById(R.id.hairColor);
hairColor.setOnItemSelectedListener(this);
List<String> hairColorArray = new ArrayList<>();
hairColorArray.add("Black");
hairColorArray.add("Blonde");
hairColorArray.add("Blue");
(...)
ArrayAdapter<String> hairColorAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairColorArray);
hairColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairColor.setAdapter(hairColorAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
if(parent.getId()==hairSpinner.getId())
type = hairTypeArray.get(position);
else
color = hairColorArray.get(position);
switch(type){
case "Bald":
imageView.setImageResource((R.drawable.sw_head));
break;
case "Female Short":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_short_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_short_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_short_blue);
else if(color=="Brown")
(...)
break;
case "Female Medium":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_med_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_med_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_med_blue);
else if(color=="Brown")
(...)
break;
case "Female Long":
(...)
default:
break;
}
}
public void onNothingSelected(AdapterView<?> parent){
}
I need to be able to know what has been selected on both spinners at the same time.
I get this error report when the activity (btw it crashes as soon as the activity opens)
04-27 15:39:17.165 9934-9934/cameron.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: cameron.myapplication, PID: 9934
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at cameron.myapplication.HeadZoom.onItemSelected(HeadZoom.java:83)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The reason is already you Initialized ArrayList in global .
private List<String> hairTypeArray = new ArrayList<String>();
private List<String> hairColorArray = new ArrayList<String>();
so out of oncreate method its take only global variable values.but you added values only local arraylist oncreate method.so remove the following code in oncreate method
List<String> hairColorArray = new ArrayList<>();
List<String> hairTypeArray = new ArrayList<String>();