1

我有一个 BaseAdaptor,我已经扩展它来处理一些对象(Spinner、切换视图等)。我正在尝试从微调器中获取选定的值。我的问题是 getSelectedItem() 总是返回第一个值(索引 0)。不知道我错过了什么。这是我的代码。

GridView grid=(GridView)findViewById(R.id.grdMain);
AcmObjectBaseAdaptor ad = (AcmObjectBaseAdaptor) grid.getAdapter();
String year = "";
if (ad.getItem(0) instanceof Spinner){
    Spinner spinner = (Spinner) ad.getItem(0);
    year = spinner.getSelectedItem().toString();
    System.out.println("Selected: " + year);
}
4

1 回答 1

1

如果将和 onItemSelectedListener 添加到微调器会怎样?

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
         System.out.println("onItemSelected: " + position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {}
});
于 2011-12-27T00:09:27.577 回答