Android drop-down list display

http://blog.csdn.net/a_fyf/article/details/51674790

Don't bother to move, just move the connection.

Run to display the drop-down list, click to expand, click the sub item to display the sub item on the horizontal line, and display the selected city in the TextView.


1. Add the Spinner control to the activity main.xml page.

Activity main.xml page:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tvShow"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:textSize="20sp" />  
  12.   
  13.     <Spinner  
  14.         android:id="@+id/spDwon"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_alignParentLeft="true"  
  18.         android:layout_below="@+id/tvShow"  
  19.         android:layout_marginTop="36dp" />  
  20.   
  21. </RelativeLayout>  


2. The mainactivity.java page defines the data source and configures the adapter.
MainActivity.java page:
  1. package com.example.downshow;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.os.Bundle;  
  7. import android.app.Activity;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.widget.AdapterView;  
  12. import android.widget.ArrayAdapter;  
  13. import android.widget.Spinner;  
  14. import android.widget.TextView;  
  15. import android.widget.AdapterView.OnItemSelectedListener;  
  16.   
  17. public class MainActivity extends Activity implements OnItemSelectedListener{  
  18.   
  19.     private TextView tvShow;  
  20.     private Spinner spDown;  
  21.     private List<String> list;  
  22.     private ArrayAdapter<String> adapter;  
  23.       
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.       
  29.         tvShow=(TextView) findViewById(R.id.tvShow);  
  30.         spDown=(Spinner) findViewById(R.id.spDwon);  
  31.         tvShow.setText("The city of your choice is Beijing");  
  32.           
  33.         /*set up data sources*/  
  34.         list=new ArrayList<String>();  
  35.         list.add("Beijing");  
  36.         list.add("Shanghai");  
  37.         list.add("Guangzhou");  
  38.         list.add("Shenzhen");  
  39.           
  40.         /*New adapter*/  
  41.         adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);  
  42.       
  43.         /*adapter Set a drop-down list style, and the parameters are system sub layout*/  
  44.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
  45.       
  46.         /*spDown Load adapter*/  
  47.         spDown.setAdapter(adapter);  
  48.       
  49.         /*soDown Listener*/  
  50.         spDown.setOnItemSelectedListener(this);  
  51.     }  
  52.   
  53.     @Override  
  54.     public boolean onCreateOptionsMenu(Menu menu) {  
  55.         getMenuInflater().inflate(R.menu.main, menu);  
  56.         return true;  
  57.     }  
  58.   
  59.     @Override  
  60.     public void onItemSelected(AdapterView<?> parent, View view, int position,  
  61.             long id) {  
  62.         String cityName=adapter.getItem(position);   //Get the selected item  
  63.         tvShow.setText("The city you chose is"+cityName);  
  64.     }  
  65.   
  66.     @Override  
  67.     public void onNothingSelected(AdapterView<?> arg0) {  
  68.     }  
  69. }  


3. Run to display the drop-down list of text, but you can also customize the drop-down list style.


List of pictures and texts
Target effect:
     


1. Create a new item.xml page as a sub layout.
item.xml page:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/ivFruit"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/ic_launcher" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/tvFruit"  
  15.         android:layout_gravity="center"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="TextView" />  
  19.   
  20. </LinearLayout>  


2. Create a new second activity.java page and activity_second.xml page to display the drop-down list of images and texts.
activity_second.xml page:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".SecondActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tvShow"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello_world" />  
  12.   
  13.     <Spinner  
  14.         android:id="@+id/spDown"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_alignParentRight="true"  
  18.         android:layout_below="@+id/textView1"  
  19.         android:layout_marginTop="35dp" />  
  20.   
  21. </RelativeLayout>  


3. The secondactivity.java page setup adapter adds data sources.
SecondActivity.java page:
  1. package com.example.downshow;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemSelectedListener;  
  14. import android.widget.ImageView;  
  15. import android.widget.SimpleAdapter;  
  16. import android.widget.Spinner;  
  17. import android.widget.TextView;  
  18.   
  19. public class SecondActivity extends Activity implements OnItemSelectedListener{  
  20.   
  21.     private TextView tvShow;  
  22.     private Spinner spDown;  
  23.   
  24.     private SimpleAdapter adapter;  
  25.     private List<Map<String, Object>> dataList;  
  26.   
  27.     private int[] icon = { R.drawable.apple_pic, R.drawable.banana_pic,  
  28.             R.drawable.cherry_pic, R.drawable.grape_pic, R.drawable.mango_pic,  
  29.             R.drawable.orange_pic, R.drawable.pear_pic,  
  30.             R.drawable.pineapple_pic, R.drawable.strawberry_pic,  
  31.             R.drawable.watermelon_pic };  
  32.     private String[] iconName = { "Apple""Banana""Cherry""Grape""Mango""A mandarin orange""Pear",  
  33.             "pineapple""Strawberry""watermelon" };  
  34.   
  35.     @Override  
  36.     protected void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.activity_second);  
  39.   
  40.         tvShow = (TextView) findViewById(R.id.tvShow);  
  41.         spDown = (Spinner) findViewById(R.id.spDown);  
  42.         tvShow.setText("You chose apple");  
  43.   
  44.         /*create data source*/  
  45.         dataList = new ArrayList<Map<String, Object>>();  
  46.           
  47.         /*Create a simple adapter*/  
  48.         adapter = new SimpleAdapter(this, getData(), R.layout.item,  
  49.                 new String[] { "ivFruit""tvFruit" }, new int[] { R.id.ivFruit,  
  50.                         R.id.tvFruit });  
  51.           
  52.         /*adapter Set a drop-down list style, and the parameter is the sub layout defined by itself*/  
  53.         adapter.setDropDownViewResource(R.layout.item);  
  54.           
  55.         /*spDown Load adapter*/  
  56.         spDown.setAdapter(adapter);  
  57.           
  58.         /*spDown Set listening events*/  
  59.         spDown.setOnItemSelectedListener(this);  
  60.     }  
  61.   
  62.     /*set up data sources*/  
  63.     private List<Map<String, Object>> getData() {  
  64.         for(int i=0;i<icon.length;i++){      //Loop to add picture text message  
  65.             Map<String,Object> map=new HashMap<String, Object>();  
  66.             map.put("ivFruit", icon[i]);  
  67.             map.put("tvFruit",iconName[i]);  
  68.             dataList.add(map);  
  69.         }  
  70.         return dataList;  
  71.     }  
  72.   
  73.     @Override  
  74.     public boolean onCreateOptionsMenu(Menu menu) {  
  75.         getMenuInflater().inflate(R.menu.second, menu);  
  76.         return true;  
  77.     }  
  78.   
  79.     @Override  
  80.     public void onItemSelected(AdapterView<?> parent, View view, int position,  
  81.             long id) {  
  82.         tvShow.setText("You chose"+adapter.getItem(position));//Change display information  
  83.     }  
  84.   
  85.     @Override  
  86.     public void onNothingSelected(AdapterView<?> arg0) {  
  87.         // TODO Auto-generated method stub  
  88.     }  
  89. }  


4. Run to display the target effect of the image and text.

Tags: Android Java xml encoding

Posted on Wed, 01 Apr 2020 22:45:32 -0700 by Jumba