setOnItemSelectedListener.
ArrayList<String> ad_days = new ArrayList<String>();
int ad_endDays = 31;
for (int i = 1; i <= ad_endDays; i++) {
ad_days.add(Integer.toString(i));
}
ArrayAdapter<String> ad_daysAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ad_days);
spnday2.setAdapter(ad_daysAdapter);
spnday2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object objDay2 = parent.getItemAtPosition(position);
CharSequence charDay2= (CharSequence) objDay2;
String sDay2= (String) charDay2;
int iDay2 = Integer.parseInt(sDay2.toString());
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),iDay2 +" this Day",Toast.LENGTH_SHORT).show();
}
Spinner displays day of month in int, but when I click button I'm not able to access Day from outside of the onItemSelectedListner.
Spinner在int中显示日期,但是当我单击按钮时,我无法从onItemSelectedListner外部访问Day。
1 个解决方案
#1
1
Instead of declaring it inside onItemSelected. Make it field variable of your activity.
而不是在onItemSelected中声明它。使其成为您活动的字段变量。
Eg.
public class MyActivity extends AppCompatActivity{
private int iDay2;
//Your Code
}
#1
1
Instead of declaring it inside onItemSelected. Make it field variable of your activity.
而不是在onItemSelected中声明它。使其成为您活动的字段变量。
Eg.
public class MyActivity extends AppCompatActivity{
private int iDay2;
//Your Code
}