每个产品我都有一个微调器。微调器有产品选项(如重量 10 磅、20 磅等),也有二级选项,如颜色。
为此,我想让客户在用颜色绑定相同的微调器后选择第一个重量。但我不能让微调器启动 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);
}