Search code examples
androidandroid-spinner

Best practice to implement key-value pair in android Spinner


I want to implement an android spinner with key value .

en:english jp:japanese

And when user select and item I want to save key to database . And also on next load I want spinner to select a particulate spinner position.

Can any one please tell me what is the best way to implement this.

I have tried following but doesn't met my requirement https://gist.github.com/granoeste/2786663 Set Key and Value in spinner

Write now I am using LinkedHashMapAdapter. But issue is that here I need to store position in DB or local shared preference . I don't think this a better solution

Current Implementation

LinkedHashMap localeList = new LinkedHashMap<>();

        for (Map.Entry<String, String> val : map.entrySet()) {
            int resourceId = getResources().getIdentifier(val.getValue(), "string", getPackageName());
            localeList.put(val.getKey(), getResources().getString(resourceId));

        }
        LinkedHashMapAdapter<String, String> arrayAdapter = new LinkedHashMapAdapter<>(this, R.layout.spinner_layout, localeList);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        mLocale.setAdapter(arrayAdapter);
        String localPosSelected = PreferencesHelper.getSharedPreferenceString(this, Constants.PREF_LOCALE_POS_KEY);
        if(localPosSelected!=null){
            mLocale.setSelection(Integer.parseInt(localPosSelected));
        }

Also is there an option to selected item of Spinner by value


Solution

  • setup Spinner:

            spLang = (Spinner)view.findViewById( R.id.spLang );
            spLang.setOnItemSelectedListener( this );
            ArrayList<String> sp_Lang = new ArrayList<String>();
            sp_Lang.add("english");
            sp_Lang.add("french");
            ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, sp_Lang);
            spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spLang.setAdapter(spinnerAdapter);
    

    For Perticular Spinner item Selection:

    int Position = spinnerAdapter.getPosition("Japanese");
    spLang.setSelection(Position);
    

    First Create Hashmap for store Key and value pair

    HashMap<String ,String> hmLang = new HashMap<String,String>();
    

    Now Add value like this into HashMap :

    hmLang.put("english" ,"en");
    

    Here in HashMap key = YourValue and Value = yourKey

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        switch(adapterView.getId())
        {
            case R.id.spLang:
            String lang_Name = adapterView.getItemAtPosition(position).toString();
            String lang_Key = hmLang.get(lang_Name);
            break
    
        }
    

    If you have any issue with this code then please ask