My code is very repetitive and I want to know if someone have a better way to do it.
Would it be possible to create a variable of R.array.spinner_marque
?
EDIT: I have two spinners and when I click on marque my other spinner gives me other data. The problem is I need to copy the same code for every brands.
switch (String.valueOf(parent.getSelectedItem())) {
case "Dunlop":
dunlop();
break;
case "Falken":
falken();
break;
case "Firestone":
firestone();
break;
case "General Tire":
general_tire();
break;
private void marque() {
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
R.array.spinner_marque, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner_marque.setAdapter(adapter);
}
private void dunlop() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
R.array.spinner_dunlop, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_serie.setAdapter(adapter);
}
private void falken() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
R.array.spinner_falken, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_serie.setAdapter(adapter);
}
private void firestone() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
R.array.spinner_firestone, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_serie.setAdapter(adapter);
}
You can create one method that creates all these adapters
private ArrayAdapter<CharSequence> createFrom(@ArrayRes int arrayRes) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
arrayRes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return adapter;
}
Then you can use it in your code like
private void marque() {
spinner_marque.setAdapter(createFrom(R.array.spinner_marque));
}
Or
private void marque() {
ArrayAdapter<CharSequence> adapter = createFrom(R.array.spinner_marque);
spinner_marque.setAdapter(adapter);
}