Android动态ArrayAdapter Spinner

时间:2014-04-19 06:03:11

标签: android android-listview android-arrayadapter android-spinner

我有两个微调器,A和B.B的微调器选项根据所选的选项A而改变。

我是怎么做的:

            final ObjA[] ObjAArray = // db call
    SpinAdapter<ObjA> sObjAAdapter = new SpinAdapter<ObjA>(this, android.R.layout.simple_spinner_item, ObjAArray);
    sObjA.setAdapter(sObjAAdapter);

    sObjA.setOnItemSelectedListener(new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view,
                  int position, long id) {
              // Here you get the current item (a User object) that is selected by its position
              ObjA ObjA = ObjAArray[position];
              final ObjB[] ObjBArray = // db call
              SpinAdapter<ObjB> sObjBAdapter = new SpinAdapter<ObjB>(Activity.this,
                  android.R.layout.simple_spinner_item, ObjBArray);
              sObjB.setAdapter(sObjBAdapter);
          }
          @Override
          public void onNothingSelected(AdapterView<?> adapter) {  }
      });

这很好但我需要在开头选择A和B为特定值。

        ObjB oldObjB = // db call
  ObjA oldObjA = // db call

  int ObjAIndexSpinner = Arrays.asList(ObjAArray).indexOf(oldObjA);
  sObjA.setSelection(ObjAIndexSpinner);

  final ObjB[] ObjBArray = DBObjB.getAllObjB(Activity.this, oldObjA.getID());
  int ObjBIndexSpinner = Arrays.asList(ObjBArray).indexOf(oldObjB);
  sObjB.setSelection(1);

第一个微调器设置正确但第二个没有。它默认为第0个索引。我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

我刚刚制作了一个测试程序,以了解发生了什么......布局只包含两个微调器sp1和sp2:

private final int START_SELECTION_SPINNER1 = 0;
private final int START_SELECTION_SPINNER2 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Spinner sp1 = (Spinner)findViewById(R.id.sp1);
    final Spinner sp2 = (Spinner)findViewById(R.id.sp2);

    List<String> sp1List = new ArrayList<String>();
    sp1List.add("Selection 1 Choice 1");
    sp1List.add("Selection 1 Choice 2");

    final Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
    map.put(0, new ArrayList<String>() {{
        add("SubSelection 1 Choice 1");
        add("SubSelection 1 Choice 2");
    }});
    map.put(1, new ArrayList<String>() {{
        add("SubSelection 2 Choice 1");
        add("SubSelection 2 Choice 2");
    }});

    final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,sp1List);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,new ArrayList<String>());
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    sp1.setAdapter(adapter1);
    sp2.setAdapter(adapter2);

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {

            adapter2.clear();
            adapter2.addAll(map.get(position));
            adapter2.notifyDataSetChanged();

        }

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

    });

    sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {

                     //DO YOUR STUFF

        }

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

    });

    final Handler handler = new Handler();
    handler.post(new Runnable() {

        @Override
        public void run() {
            sp1.setSelection(START_SELECTION_SPINNER1);     
            handler.post(new Runnable() {

                @Override
                public void run() {
                    sp2.setSelection(START_SELECTION_SPINNER2);     
                }
            });
        }
    });

}

重要的是最后几行,我有处理程序发布setSelection()。现在它有效:)