Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner with Key-Value Pair

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>बाजार</item>
                  <item>वस्तु</item>


            </string-array>

     </resources>
like image 693
Pawan Gupta Avatar asked Sep 06 '13 10:09

Pawan Gupta


1 Answers

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) {    
            }
        });
like image 190
Sudhir Singh Avatar answered Oct 11 '22 11:10

Sudhir Singh