无法检索微调器的值

时间:2017-10-11 05:23:22

标签: android android-spinner

我正在为我的应用实现一个自定义微调器,我使用隐藏字段值。它正在工作但是当我再次更新时,第一次保存的项目没有显示在微调器上我需要它将显示在微调器上。谢谢,下面是我的代码。

    final MyData items[] = new MyData[25];
    items[14] = new MyData("5ft 1in","154");
    items[15] = new MyData("5ft 2in","157");
    items[16] = new MyData("5ft 3in","160");
    items[17] = new MyData("5ft 4in","162");
    items[18] = new MyData("5ft 5in","165");
    items[19] = new MyData("5ft 6in","167");
    items[20] = new MyData("5ft 7in","170");
    items[21] = new MyData("5ft 8in","172");
    items[22] = new MyData("5ft 9in","175" );
    items[23] = new MyData("5ft 10in","177");
    items[24] = new MyData("5ft 11in","180");
    items[25] = new MyData("5ft 12in","182");
    items[26] = new MyData("6ft 1in","185");
    items[27] = new MyData("6ft 2in","187");
    items[28] = new MyData("6ft 3in","190");
    items[29] = new MyData("6ft 4in","193");
    items[30] = new MyData("6ft 5in","195");
    items[31] = new MyData("6ft 6in","198");
    items[32] = new MyData("6ft 7in","201");
    items[33] = new MyData("6ft 8in","203");
    items[34] = new MyData("6ft 9in","206");
    items[35] = new MyData("6ft 10in","208");
    items[36] = new MyData("6ft 11in","211");
    items[37] = new MyData("6ft 12in","213");
    items[38] = new MyData("Above 7ft","217");
    ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(this, android.R.layout.simple_spinner_item, items );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinHeight1.setAdapter(adapter);
    for(int i=0; i < adapter.getCount(); i++) {
        if (partnerHeight1.trim().equalsIgnoreCase(adapter.getItem(i).toString())) {
            spinHeight1.setSelection(i);
            break;
        }
    }

    spinHeight1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            MyData d = items[i];
            heightFrom=d.getValue();
        }

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

        }
    });

3 个答案:

答案 0 :(得分:1)

试试这种方式

 spinHeight1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

heightFrom=spinHeight1.getSelectedItem().toString();
        }

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

        }
    });

答案 1 :(得分:1)

如果partnerHeight1是字符串,那么

for(int i=0; i < items.length; i++) {

        if (partnerHeight1.trim().equalsIgnoreCase(items[i].getValue())) {
            spinHeight1.setSelection(i);
            break;
        }
    }

如果partnerHeight1是MyData,那么

for(int i=0; i < items.length; i++) {

        if (partnerHeight1.getValue().equalsIgnoreCase(items[i].getValue())) {
            spinHeight1.setSelection(i);
            break;
        }
    }

答案 2 :(得分:0)

如果要更改适配器的数据,以便将该数据反映到视图{{1}}

void notifyDataSetChanged()

  

通知附加的观察者基础数据已更改,反映数据集的任何视图都应自行刷新。