If you don't talk much about it, attach it first. The effect is as follows:
I. I want to achieve this effect:
1. When selecting a province in the first spinner, both the second spinner and edittext will be synchronized and the values of the two will be the same;
2. The value of edittext is also changed and the value of edittext is the same when you select the second spinner.
II. My confusions:
I can implement the second-level interaction of the spinner, but how can I change the value of edittext when the options of the two spinner change?
I was a bit bored. I found some information from the Internet and did not find a solution. However, I insisted that the last user reminded me that it provided me with ideas and found me
Here is a solution to the problem. Thank you again!
3. Solution and code (here I only paste the main code ):
1) Layout file code, that is, the layout code of the figure above:
<? XML version = "1.0" encoding = "UTF-8"?>
<Scrollview Android: Id = "@ + ID/weather_query"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" xmlns: Android = "http://schemas.android.com/apk/res/android">
<Linearlayout Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Linearlayout Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0">
<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Province"/>
<Spinner
Android: Id = "@ + ID/spiner01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "city"/>
<Spinner
Android: Id = "@ + ID/spiner02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Linearlayout>
</Linearlayout>
<Framelayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0">
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Edittext
Android: Id = "@ + ID/edittext"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: hint = "Chinese or full spelling (for example, Shandong or Shandong )"
Android: singleline = "true"
Android: layout_weight = "1.0"/>
<Button
Android: Id = "@ + ID/query_button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/OK"/>
</Linearlayout>
</Framelayout>
<Textview Android: textcolor = "# ffff0000"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Linearlayout Android: layout_width = "wrap_content"
Android: layout_height = "10.0dip"/>
<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_margintop = "-20.0px"
Android: text = "@ string/tips_1"/>
</Linearlayout>
</Scrollview>
2) activity code:
Package com. Test. PP;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. edittext;
Import Android. widget. spinner;
Import Android. widget. adapterview. onitemselectedlistener;
Public class weathercitylist extends activity {
Private spinner prov_spr;
Private spinner city_spr;
Arrayadapter <string> adapter01;
Arrayadapter <string> adapter02;
Private string [] [] arrayofstring1 = constdata. City;
Private edittext;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. weather_city_list );
String [] arrayofstring = constdata. province;
Adapter01 = new arrayadapter <string> (this,
Android. R. layout. simple_spinner_item, arrayofstring );
Adapter01.setdropdownviewresource (Android. R. layout. simple_dropdown_item_1line );
Prov_spr = (spinner) This. findviewbyid (R. Id. spiner01 );
Prov_spr.setadapter (adapter01 );
Prov_spr.setonitemselectedlistener (selectlistener );
City_spr = (spinner) This. findviewbyid (R. Id. spiner02 );
City_spr.setadapter (adapter02 );
Edittext = (edittext) findviewbyid (R. Id. edittext );
City_spr.setonitemselectedlistener (selectlistener01); // It is used to process result 2 code and listen to events.
}
Private onitemselectedlistener selectlistener = new onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Parent, view V, int position,
Long ID ){
Int Pos = prov_spr.getselecteditemposition ();
Adapter02 = new arrayadapter <string> (weathercitylist. This,
Android. R. layout. simple_spinner_item, arrayofstring1 [POS]);
City_spr.setadapter (adapter02 );
Edittext. settext (city_spr.getselecteditem (). tostring (); // This line of code is used to synchronize the value in edittext with the value of the previous two spinner, and write the value of the city we get into edittext.
}
@ Override
Public void onnothingselected (adapterview <?> Arg0 ){
}
};
Private onitemselectedlistener selectlistener01 = new onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Parent, view V, int position,
Long ID ){
Edittext. settext (city_spr.getselecteditem (). tostring (); // The value in edittext is also changed when we select this parameter.
}
@ Override
Public void onnothingselected (adapterview <?> Arg0 ){
}
};
}