I am creating a translator app in which user need to choose the target language if suppose he choose French from the spinner then value for the FRENCH should be 'fr' so i can pass that value in url. I have created a spinner and assigned languages from array adapter but I want to use it's short form like for French the value should be fr how can I achieve that?
"French","fr" "Arabic", "ar" "English", "en"
my app shows the language list KEY and app crahes when translate button is pressed here is my code, what next
Spinner spinner = findViewById(R.id.spinner);
Map<String, String> flanguages = new HashMap<String, String>();
flanguages.put("Arabic", "ar");
flanguages.put("English", "en");
flanguages.put("French", "fr");
flanguages.put("Hausa", "ha");
flanguages.put("Igbo", "ig");
flanguages.put("Yoruba", "yo");
List<String> list = new ArrayList<String>(flanguages.keySet());
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
Easiest way to do is to create a LanguageItem Class.
class LanguageItem{
String language;
String isoCode;
override String toString(){
return language;
}
}
Use this class to add your language and its iso code. Put it in ArrayList and show it in spinner.
ArrayAdapter<LanguageItem> adapter = new ArrayAdpter(this, android.R.layout.simple_spinner_item, arraylist)
Now onItemSelected you can easily get selected LanguageItem with the position.