public class layout8 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextView tv7;
private Spinner sp;
private ArrayAdapter adapter;
private List<String> list=new ArrayList<String>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout7);
sp= (Spinner) findViewById(R.id.spinner_1);
tv7= (TextView) findViewById(R.id.textview_7);
adapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
sp.setAdapter(adapter);
list.add("北京");
list.add("上海");
sp.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String city=(String) adapter.getItem(i);
tv7.setText(city);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}