开发者

Android 2.2 SDK - Create Spinner from Map?

Basically, I want to associate a "selected" option with an id, so instead of doing this (my current way):

            Vector spinnerList = new Vector();
            spinnerList.addElement("No");
            spinnerList.addElement("Yes");

I'd be doing something like this (the Hashtable/Vector is just for Blackberry开发者_运维技巧 compatability):

            String id = "3";
            Hashtable spinnerMap = new Hashtable();
            spinnerMap.put(id, "No");
            spinnerMap.put(id, "Yes");

Currently, a selected "option" from the spinner prints out 0 or 1 (based on the "No", "Yes"). So, my question is, if I'm setting the spinners programatically from a map whose values I don't know (I just know ids), how do I do this?

            Spinner spinner = new Spinner(this);
            ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
                    android.R.layout.simple_spinner_dropdown_item, spinnerList);
            spinner.setAdapter(spinnerArrayAdapter);

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    System.out.println("Selected: " + arg2);

                }

                public void onNothingSelected(AdapterView<?> arg0) {
                    System.out.println("Nothing selected: " + arg0);
                }
            });


Your question is not very clear. What exactly do you want to accomplish?
If you just want to display different data than a simple list of strings I think you should consider creating a custom adapter.


Reverse your map, and you can do an easy lookup of the integer id based on the string returned.

int id = spinnerMap.get(spinner.getSelectedItem());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜