Spinner dropdown

Implement three drop-down buttons, requiring the selection of the first province, the second city, and the third scenic spot to track the corresponding province synchronously
XML code in the layout file in the resources folder:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.phone.day04_home02.MainActivity" >

    <Spinner
        android:id="@+id/s1_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        
        />
   
     <Spinner
        android:id="@+id/s2_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        />
      <Spinner
        android:id="@+id/s3_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
        

</LinearLayout>
The code in MainActivity uses the adapter to complete the implementation
package com.phone.day04_home02;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
	//Create three adapters
	private ArrayAdapter<String> p_adapter,c_adapter,j_adapter;
	/ / Create the corresponding resource array, the city and attractions use a two-dimensional array
	private String[] province={"Beijing","Shanghai","Shandong","Jiangsu"};
	private String[][] city={{"Haidian District","Changping District","Chaoyang District"},{"Huangpu District","Pudong New District"},{"Jinan","Qingdao","Liaocheng" },{"Suzhou","Nanjing","Changzhou"}};
	private String[][] jingdian={{"Forbidden City","Summer Palace","Yuanmingyuan"},{"The Bund","Yu Garden","Lujiazui","Nanjing Pedestrian Street"},{"Zhanqiao","Daming Lake" ","Baotu Spring"},{"Suzhou Garden","Nanjing Yangtze River Bridge","Jiangnan Town"}};
	//declare a reference to Spinner
	private Spinner s1_sp;
	private Spinner s2_sp;
	private Spinner s3_sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		s1_sp=(Spinner) findViewById(R.id.s1_sp);
		s2_sp=(Spinner) findViewById(R.id.s2_sp);
		s3_sp=(Spinner) findViewById(R.id.s3_sp);
		// build the adapter
		p_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,province);
		c_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item);
		c_adapter.addAll(city[0]);
		j_adapter=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item);
		j_adapter.addAll(jingdian[0]);
		//Link the adapter to the Spinner
		s1_sp.setAdapter(p_adapter);
		s2_sp.setAdapter(c_adapter);
		s3_sp.setAdapter(j_adapter);
		//Set the listener for the province Spinner
		s1_sp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				// clear the adapter
			    c_adapter.clear();
				j_adapter.clear();
				//Add array resources, corresponding to the position of the group
				c_adapter.addAll(city[position]);
				j_adapter.addAll(jingdian[position]);
				//Spinner default selection reset to the first one
				s2_sp.setSelection(0);
				s3_sp.setSelection (0);
				
				
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645273&siteId=291194637