由于特定原因,我需要调用spinner
的OnItemSelectedListener
,即使用户选择了相同的项目,但通常不会调用。所以我不知道这怎么可能!有什么技巧或库可以解决我的问题吗?
好的
您可以使用addOnLayoutChangeListener
yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
}
});
确保上下文正确。您可以将上下文设置为.
-
这个
-
主要活动。此
-
getApplicationContext((
如果在Fragment中使用微调器,则可以将上下文用作
getActivity((
String selectedItem;
String data[]={"Australia","Delhi","Agra","Mumbai","America","Tanzania","Arizona"};
spinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
spinner.setPrompt("Select State");
spinner.setAdapter(ad);
String val=spinner.getSelectedItem().toString();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String val=spinner.getSelectedItem().toString();
selectedItem=data[i];
Toast.makeText(MainActivity.this, " "+selectedItem, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});