Key pair value spinner

I am working on an interface of several languages. My requirement is spinner. I want to show the data in Hindi, but when it is selected, it must return to the English show, it can compare with further decision-making. Same as tag with tag.

My Java code looks something like this:

HashMap<String,String> options=new HashMap<String,String>(); String optionsEnglish [] = getResources().getStringArray(R.array.option_array); String optinsHindi[]= getResources().getStringArray(R.array.option_array_hindi); for(int i=0;i<optionsEnglish.length;i++) { options.put(optionsEnglish[i], optinsHindi[i]); } Spinner optionSpinner = (Spinner) findViewById(R.id.optionPicker); ArrayAdapter<HashMap<String, String>> dataAdapter = new ArrayAdapter<HashMap<String,String>>(this, android.R.layout.simple_spinner_item); dataAdapter.add(options); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); optionSpinner.setAdapter(dataAdapter); 

In xml

  <resource> <string-array name="option_array"> <item>Market</item> <item>Commodity</item> </string-array> <string-array name="option_array_hindi"> <item>बाजार</item> <item>वस्तु</item> </string-array> </resources> 
+4
source share
5 answers

Hope this helps you.

Add data to Spinner

 private void setDataInSpinner(Spinner id, int dataArray) { ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, dataArray, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_item); id.setAdapter(adapter); } 

To get the selected Spinner value, use

 Spinner mySpinner = (Spinner)findViewById(R.id.spinner); String Text = mySpinner.getSelectedItem().toString(); 

Or you can use setOnItemSelectedListener to get the selected values

 String value = GetClassCode.getCode(Text);//here u have to pass the value that is selected on the spinner 

Create Class

 public class GetClassCode { static HashMap<String, String> codeHash = new HashMap<String, String>(); static { init(); } public static void init() { codeHash.put("key", "value"); codeHash.put("key", "value"); codeHash.put("key", "value"); codeHash.put("key", "value"); } public static String getCode(String param) { return codeHash.get(param); } } 
+2
source

You created two separate string arrays for language words like the specified xml, right? then just use the ArrayAdapter for the first array of parameters and set the adapter to spinner. and when you select any counter element in accordance with it, the position selects the world from the second array of strings.

it will be quite simple and also work in more than 2 languages.

+1
source

I created a HashMap adapter that can help. Also see sample project here

  mapData = new LinkedHashMap<String, String>(); mapData.put("shamu", "Nexus 6"); mapData.put("fugu", "Nexus Player"); mapData.put("volantisg", "Nexus 9 (LTE)"); mapData.put("volantis", "Nexus 9 (Wi-Fi)"); mapData.put("hammerhead", "Nexus 5 (GSM/LTE)"); mapData.put("razor", "Nexus 7 [2013] (Wi-Fi)"); mapData.put("razorg", "Nexus 7 [2013] (Mobile)"); mapData.put("mantaray", "Nexus 10"); mapData.put("occam", "Nexus 4"); mapData.put("nakasi", "Nexus 7 (Wi-Fi)"); mapData.put("nakasig", "Nexus 7 (Mobile)"); mapData.put("tungsten", "Nexus Q"); adapter = new LinkedHashMapAdapter<String, String>(this, android.R.layout.simple_spinner_item, mapData); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); 
+1
source

A bit old question, but this is in the top answers when searching on google.

I used this method:

Get the localized value from the counter:

 String localizedValue = ((Spinner) findViewById(R.id.mySpinner)).getSelectedItem().toString(); 

Then dynamically extract the key from the line resource file:

 String key = (String) getResources().getText(getResources().getIdentifier(localizedValue, "string", "my.package.name")); 

All languages ​​must contain the key for localizedValue in strings.xml:

 बाजार=Market 
0
source

Step 1: Create a POJO class that takes care of the key and value

  public class Country { private String id; private String name; public Country(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } //to display object as a string in spinner @Override public String toString() { return name; } @Override public boolean equals(Object obj) { if(obj instanceof Country){ Country c = (Country )obj; if(c.getName().equals(name) && c.getId()==id ) return true; } return false; } } 

Step 2: Prepare Data for Upload to the Counter

 private void setData() { ArrayList<Country> countryList = new ArrayList<>(); //Add countries countryList.add(new Country("1", "India")); countryList.add(new Country("2", "USA")); countryList.add(new Country("3", "China")); countryList.add(new Country("4", "UK")); //fill data in spinner ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(context, android.R.layout.simple_spinner_dropdown_item, countryList); sp_country.setAdapter(adapter); sp_country.setSelection(adapter.getPosition(myItem));//Optional to set the selected item. } 

Step 3: and finally, get the key and value of the selected item in the listener onitemselected listener method

 sp_country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Country country = (Country) parent.getSelectedItem(); Toast.makeText(context, "Country ID: "+country.getId()+", Country Name : "+country.getName(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 
0
source

Source: https://habr.com/ru/post/1500904/


All Articles