Android系统。选择微调器项时,OnItemSelectedListener不起作用

时间:2017-05-23 12:29:38

标签: android

我有一个片段,我动态创建微调器。当我从微调器中选择项目时,onItemSelected不会触发。我的方法可以使用AdapterView.OnItemSelectedListener界面吗?我究竟做错了什么?是否有处理项目选择的最佳实践?

以下是代码:

public class ArticleAddParamsFragment extends Fragment implements AdapterView.OnItemSelectedListener {

    private LinearLayout addPrLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_article_addparams, container, false);
        addPrLayout = (LinearLayout) rootView.findViewById(R.id.addparams_layout);
        setUpSpinners(getArguments().getLong("idArt"));
        return rootView;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // doesn't go here
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // doesn't go here

    }

    private void setUpSpinners(long idArt){
        RealmAddParam addParForArt = realm.where(RealmAddParam.class)
                .findAll()
                .where()
                .equalTo("idTaskart", idArt)
                .findFirst();
        for (RealmAddPrmMain prm: addParForArt.getAddParams()) {
            addPrLayout.addView(createSpinner(prm));
        }
    }

    public Spinner createSpinner(RealmAddPrmMain adPrm) {
        Spinner spinner = new Spinner(getActivity());
        spinner.setPrompt(adPrm.getNameAdd());
        spinner.setFocusableInTouchMode(true);
        spinner.setFocusable(true);
        final ArrayList<SpinnerAdPrObj> spObjects = new ArrayList<>();
        if (adPrm.getVvalue() != null) {
            String[] values = adPrm.getVvalue().replace("[","").replace("]","").split(",");
            if (values.length > 1){
                for (Integer i = 0; i < values.length; i++){;
                    if (values[i].split(":::").length > 1) {
                        spObjects.add(new SpinnerAdPrObj(values[i].split(":::")[0], values[i].split(":::")[2], values[i].split(":::")[1]));
                    } else {
                        spObjects.add(new SpinnerAdPrObj(values[i].split(":::")[0]));
                    }
                }
            }
        }
        ArrayAdapter<SpinnerAdPrObj> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, spObjects);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        return spinner;
    }

}

解决方案是: 只是添加spinner.setOnItemSelectedListener(this);

1 个答案:

答案 0 :(得分:1)

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

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