I'm stuck on how I can reduce this code duplication, I'm using the TextToSpeech engine and using locales so the user can select their language.
language
is a Spinner.
language.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long id) {
System.out.println(parent.getItemAtPosition(pos).toString());
if (parent.getItemAtPosition(pos).toString().equals("UK")) {
textToSpeech = new TextToSpeech(MainActivity.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
}
}
});
} else if (parent.getItemAtPosition(pos).toString()
.equals("US")) {
textToSpeech = new TextToSpeech(MainActivity.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.US);
}
}
});
} else if (parent.getItemAtPosition(pos).toString()
.equals("French")) {
textToSpeech = new TextToSpeech(MainActivity.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.FRANCE);
}
}
});
} else if (parent.getItemAtPosition(pos).toString()
.equals("Italian")) {
textToSpeech = new TextToSpeech(MainActivity.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech
.setLanguage(Locale.ITALIAN);
}
}
});
} else if (parent.getItemAtPosition(pos).toString()
.equals("German")) {
textToSpeech = new TextToSpeech(MainActivity.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech
.setLanguage(Locale.GERMAN);
}
}
});
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}