7

I am new to Android development. I am trying to populate a spinner by using the SimpleAdapter. But spinner's list is showing blank element. When I click any element, its text is shown properly in Toast. Please tell me what is the problem in my code below.

 public void onCreate(Bundle savedInstanceState) {

  private List<Map<String, String>> data = new ArrayList<Map<String, String>>();

  String[] from = new String[] { "colorsData" };
  int[] to = new int[] { R.id.spinner };

  String[] colors = getResources().getStringArray(R.array.colorsData);

  for (int i = 0; i < colors.length; i++) {
   data.add(addData(colors[i]));
  }

  Spinner spinner = (Spinner) findViewById(R.id.spinner);

  SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, android.R.layout.simple_spinner_item, from, to);
  simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(simpleAdapter);

  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    Toast.makeText(
      parent.getContext(),
      "Selected Color:-  "
        + parent.getItemAtPosition(position),
      Toast.LENGTH_LONG).show();
   }
  });
 }

 private Map<String, String> addData(String colorName) {
  Map<String, String> mapList = new HashMap<String, String>();
  mapList.put("colorsData", colorName);
  return mapList;
 }
Justin
  • 84,773
  • 49
  • 224
  • 367
Chromium
  • 1,073
  • 1
  • 9
  • 25
  • With a short glance at your question, i'm not sure what you really want to get. But if you want some UI elements like configure page in Android. i.e: click an item in a list view and pop a dialog to select something, you can try 'Preference'. Preference is a series UI elements to configure your app. – kevin lynx Dec 08 '10 at 05:45

1 Answers1

5

I'm about 95% sure that your to array should be declared as:

  int[] to = new int[] { android.R.id.text1 };

Give that a try.


EDIT (based on comments below):

It seems there was a bug in older versions of AndroidOS which caused that IllegalStateException. (I didn't see the exception in 2.2, but I did see it in 1.5 in the emulator.) The bug can be worked around by adding a ViewBinder to the SimpleAdapter. ViewBinder isn't hard to implement; here's an example:

    SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {

        public boolean setViewValue(View view, Object data,
                String textRepresentation) {
            // We configured the SimpleAdapter to create TextViews (see
            // the 'to' array), so this cast should be safe:
            TextView textView = (TextView) view;
            textView.setText(textRepresentation);
            return true;
        }
    };
    simpleAdapter.setViewBinder(viewBinder);

I blogged about this here.

Dan Breslau
  • 11,472
  • 2
  • 35
  • 44
  • I tried it. But as soon as I click the spinner dropdown button, it is crashing with an IllegalStateException. – Chromium Dec 08 '10 at 03:55
  • But are you actually seeing the text labels now? I think that that exception means that something changed in the `data` list after you created the adapter. Is that possible? – Dan Breslau Dec 08 '10 at 04:07
  • No, I am not changing the data list. I wondor now whether SimpleAdapter can be used to populate the spinner or not. – Chromium Dec 08 '10 at 04:13
  • Hmmm. I'm wondering why you don't just use `ArrayAdapter`, using the array returned from `getStringArray()`. But if you want to keep using `SimpleAdapter`, have a look at this issue ( http://code.google.com/p/android/issues/detail?id=7251 ) -- you may need to set a `ViewBinder` on the adapter. – Dan Breslau Dec 08 '10 at 04:23
  • I think, I have the only option left is to use the ArrayAdapter. This ViewBinder kind thing is a little complicated for a beginner like me. Thank you for your quick response and guidance. I hope for the same in the future. :-) – Chromium Dec 08 '10 at 04:32
  • @Chromium: ViewBinder sounds complicated, but it really isn't. See my edited comment, above. (And thanks for the Spinner example, and for giving me something to blog about ;-) – Dan Breslau Dec 08 '10 at 16:53
  • You can only use ArrayAdapter with pre-defined in XML. It doesn't work with Java List. – IgorGanapolsky Jan 04 '12 at 16:09