Using SimpleAdapter with Spinner
Asked Answered
O

1

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;
 }
Outward answered 8/12, 2010 at 3:31 Comment(1)
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.Zamia
S
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.

Shroud answered 8/12, 2010 at 3:50 Comment(7)
I tried it. But as soon as I click the spinner dropdown button, it is crashing with an IllegalStateException.Outward
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?Shroud
No, I am not changing the data list. I wondor now whether SimpleAdapter can be used to populate the spinner or not.Outward
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 ( code.google.com/p/android/issues/detail?id=7251 ) -- you may need to set a ViewBinder on the adapter.Shroud
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. :-)Outward
@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 ;-)Shroud
You can only use ArrayAdapter with pre-defined <string-array> in XML. It doesn't work with Java List.Puppy

© 2022 - 2024 — McMap. All rights reserved.