Spinner setOnItemSelectedListener不会启动

时间:2012-07-02 09:03:47

标签: android spinner

每个产品都有一个微调器。旋转器有产品选择(如重量10磅,20磅等) 还有第二级选项也像颜色。

为此,我想让客户在将相同的旋转器与颜色绑定后选择第一个重量。但我不能让spinner激活setOnItemSelectedListener。

谢谢你的时间!

ddlOptions = (Spinner) lnr.findViewById(R.id.ddlOptions);
ddlOptions.setId(products.get(i).ID);
SpinnerBind(ddlOptions, products.get(i).ID, -1);


ddlOptions.setOnItemSelectedListener(new OnItemSelectedListener() {
    int first = 0;
    int ProductId = ddlOptions.getId();

    @Override
    public void onItemSelected(AdapterView <? > parentView, View selectedItemView, int position, long id) {
        if (first != 0) {
            SpinnerBind(ddlOptions, ProductId, mItems.get(position).getId());
        } else first = 1;
    }

    @Override
    public void onNothingSelected(AdapterView <? > parentView) {
        // your code here
    }

});




public static class Element {
    private String mText;
    private int mId;

    public Element(String text, int id) {
        mText = text;
        mId = id;
    }

    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }

    public String getmText() {
        return mText;
    }

    public void setmText(String mText) {
        this.mText = mText;
    }

    @Override
    public String toString() {
        return mText;
    }
}

private List < Element > mItems;



public void SpinnerBind(Spinner ddlOptions, int ProductId, int ParentId) {
    mItems = new ArrayList < MainScreen.Element > ();
    ArrayList < String > optionNames = new ArrayList < String > ();
    ArrayList < ProductOption > productOptions = this.dhn.GetProductOptions(ProductId, ParentId);

    for (int i = 0; i < productOptions.size(); i++) {
        mItems.add(new Element(productOptions.get(i).OptionName, productOptions.get(i).ID));
    }

    ArrayAdapter < MainScreen.Element > adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, mItems);

    ddlOptions.setAdapter(adapter);
}

1 个答案:

答案 0 :(得分:2)

这是一个愚蠢的问题。只需要在微调器上的第二项。选择相同项目时,微调器不会触发。