Home > Mobile >  How to write setOnItemSelectedListener this
How to write setOnItemSelectedListener this

Time:12-23

 public class MainActivity extends the Activity {
Private Spinner Spinner=null;

//data source
Private String [] city={" 1 ", "2", "3", "4", "5", "6", "7", "eight" and "9", "10", "11", "12"};

Private List List=null;

Private ArrayAdapter adapter=null;

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the system time
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat (" yyyy ");
The Date date1=new Date (System. CurrentTimeMillis ());

String datay=simpleDateFormat1. The format (date1). The toString ();

//time? SetText (simpleDateFormat. The format (date))
SimpleDateFormat simpleDateFormat2=new SimpleDateFormat (" MM ");
The Date date2=new Date (System. CurrentTimeMillis ());
String datam=simpleDateFormat2. The format (date2). The toString ();


String n=datay. ToString ();
String y=datam. ToString ();
Toast. MakeText (this, n, Toast. LENGTH_SHORT.) the show ();
Toast. MakeText (this, y, Toast. LENGTH_SHORT.) the show ();


List=new ArrayList<> (a);
Spinner=(spinner) the findViewById (R.i d.i d_spinner);
For (int I=0; I & lt; City. Length; I++) {
List. The add (city [I]);
}
Adapter=new ArrayAdapter (this, android R.l ayout. Simple_list_item_1, a list).

Adapter. SetDropDownViewResource (android. R.l ayout. Simple_spinner_dropdown_item);

Spinner. SetAdapter (adapter);

Spinner. SetOnItemSelectedListener (new AdapterView. OnItemSelectedListener () {

});


}


Public void onItemSelected (AdapterView<?> The parent, View View, int position, long id) {
String cityName=(String) adapter. The getItem (position);

}


Public void onNothingSelected (AdapterView<?> The parent) {

}




}


CodePudding user response:

Want spinner to the system default time datam, how to write?

CodePudding user response:

 spinner. SetOnItemSelectedListener (new AdapterView. OnItemSelectedListener () {
Public void onNothingSelected (AdapterView<?> The parent) {

}
Public void onItemSelected (AdapterView<?> The parent, View View, int position, long id) {
String cityName=(String) adapter. The getItem (position);

}
});
  • Related