package com.sometrik.framework;

import java.util.ArrayList;
import java.util.TreeMap;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class FWPicker extends Spinner implements NativeMessageHandler {
  
  private Context context;
  private ArrayAdapter<String> adapter;
  private ArrayList<Integer> numberList;
  private TreeMap<Integer, String> valueMap;
  private final int id;
  
  private native void pickerOptionSelected(int id, int position);

  public FWPicker(Context context) {
    super(context);
    adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    valueMap = new TreeMap<Integer, String>();
    numberList = new ArrayList<Integer>();
    id = getId();
    this.context = context;
    
    setOnItemSelectedListener(new OnItemSelectedListener() {

      @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
	pickerOptionSelected(id, position);
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
      }
    });
  }

  public void handleCommand(NativeCommand command) {
    
    switch (command.getCommand()) {
    case ADD_OPTION:
      adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
      valueMap.put(command.getValue(), command.getTextValue());
      numberList.add(command.getValue());
      for (int i = 0; i < numberList.size(); i++) {
	adapter.add(valueMap.get(numberList.get(i)));
      }
      setAdapter(adapter);
      break;
    default:
      System.out.println("Message couldn't be handled by Picker");
      break;
    }
  }

  @Override
  public void showView() {
    System.out.println("Picker couldn't handle showView");
  }

  @Override
  public int getElementId() {
    return getId();
  }

  @Override
  public void addChild(View view) {
    System.out.println("Picker couldn't handle addChild");
    //TODO
  }

}

@@ -45,25 +45,25 @@
 va.util.
-ArrayList
+Map.Entry
 ;%0D%0Aimpor
@@ -403,50 +403,8 @@
 r;%0D%0A
-  private ArrayList%3CInteger%3E numberList;%0D%0A
   pr
@@ -742,52 +742,8 @@
 );%0D%0A
-    numberList = new ArrayList%3CInteger%3E();%0D%0A
     
@@ -1401,141 +1401,95 @@
     
-numberList.add(command.getValue());%0D%0A      for (int i = 0; i %3C numberList.size(); i++) %7B%0D%0A%09adapter.add(valueMap.get(numberList.get(i)
+for(Entry%3CInteger, String%3E entry : valueMap.entrySet()) %7B%0D%0A%09adapter.add(entry.getValue(
 ));%0D

Fix picker option sorting