6

I am working on multi languages UI. My requirement is in spinner I want to show data in Hindi but when it will be selected it should return english show it can compare to further decision making. Just like tag with tag.

My java code is 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>बाजार&lt;/item>
                  <item>वस्तु&lt;/item>


            </string-array>

     </resources>
4

5 回答 5

7

Step 1 : Create POJO class which will take care of 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 to be loaded in spinner

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 selected item's key and value in onitemselected listener method of spinner

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) {    
            }
        });
于 2018-12-08T18:29:19.427 回答
3

Hope this can help you

Add Data in in 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 value of Spinner use this

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 a 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);
    }
}
于 2013-09-06T11:02:40.880 回答
2

I created an HashMap adapter that might help. Also see example 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);
于 2015-09-19T05:49:48.863 回答
1

You made two separate string array for language words like in given xml ,right? then just use ArrayAdapter for first option array and set adapter in spinner. and on select of any item of spinner according to it's position fetch world from second string array..

it will be quite easy and also work for more then 2 languages.

于 2013-09-06T11:14:56.933 回答
0

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

I used this method:

Get the localized value from the spinner:

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

Then get the key dynamically from string resources file:

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

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

बाजार=Market
于 2015-02-06T18:03:08.833 回答