我有一个视图对象,其中包含一个EditText、一个不可见的Spinner和一个按钮。我通过LayoutInflater加载这个视图,它出现了,一切都正常工作。我点击按钮,Spinner列表出现了,我选择了我想要的项目。我的问题是Spinner的setOnItemSelectedListener没有触发,所以我无法将EditText设置为所选值。
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spn = (Spinner)sEdit.findViewWithTag("spn"+fieldID);
final String spnHolder = "spn"+fieldID;
spn.setAdapter(adapter);
ImageButton bSpn = (ImageButton)sEdit.findViewWithTag("btn"+fieldID);
bSpn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
curVw = (EditText)sEdit.findViewWithTag(v.getTag().toString().split("btn")[1]);
((Spinner)sEdit.findViewWithTag(spnHolder)).performClick();
}
});
try {
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
curVw.setText(((TextView)arg1).getText().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
我添加了一个Try...Catch语句,以查看是否仅在该点之后失败,但从未被调用。