Android学习--动态向SPinner控件中添加数据

android中的spinner动态加载内容

博客分类: Android
Android OS XML 

android中的spinner动态加载数据:

GroupPurchase.java

Java代码  
  1. package jftt.txlong;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import android.app.Activity;   
  7. import android.os.Bundle;   
  8. import android.util.Log;   
  9. import android.view.View;   
  10. import android.widget.AdapterView;   
  11. import android.widget.ArrayAdapter;   
  12. import android.widget.Button;   
  13. import android.widget.ImageView;   
  14. import android.widget.Spinner;   
  15. import android.widget.TextView;   
  16.   
  17. public class GroupPurchase extends Activity {   
  18.     private Spinner changeCity;   
  19.     private Button refresh, pre, next;   
  20.     private TextView leftTime, detail, price, citygp;   
  21.     private ImageView images;   
  22.     private ArrayAdapter adapter;   
  23.     private List allItems;   
  24.     private String[] citys = { "北京市""上海市""天津市""福州市" };   
  25.   
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {   
  28.         super.onCreate(savedInstanceState);   
  29.         setContentView(R.layout.main);   
  30.         initView();   
  31.            
  32.         allItems = new ArrayList();   
  33.         for (int i = 0; i < citys.length; i++) {   
  34.             allItems.add(citys[i]);   
  35.         }   
  36.         adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, allItems);   
  37.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
  38.         changeCity.setAdapter(adapter);   
  39.         changeCity.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {   
  40.                     @Override  
  41.                     public void onItemSelected(AdapterView arg0, View arg1,   
  42.                             int arg2, long arg3) {   
  43.                         citygp.setText(changeCity.getSelectedItem().toString() + "今天的团购");   
  44.                         Log.i("info-----------", changeCity.getSelectedItem().toString());   
  45.                     }   
  46.   
  47.                     @Override  
  48.                     public void onNothingSelected(AdapterView arg0) {   
  49.                     }   
  50.                 });   
  51.   
  52.         pre.setOnClickListener(new View.OnClickListener() {   
  53.             @Override  
  54.             public void onClick(View v) {   
  55.                 Log.i("info-----------""prefer button has pressed!!!");   
  56.             }   
  57.         });   
  58.   
  59.         next.setOnClickListener(new View.OnClickListener() {   
  60.             @Override  
  61.             public void onClick(View v) {   
  62.                 Log.i("info-----------""next button has pressed!!!");   
  63.             }   
  64.         });   
  65.     }   
  66.   
  67.     private void initView() {   
  68.         changeCity = (Spinner) findViewById(R.id.changeCity);   
  69.         citygp = (TextView)findViewById(R.id.citygp);   
  70.         refresh = (Button) findViewById(R.id.refresh);   
  71.         pre = (Button) findViewById(R.id.pre);   
  72.         next = (Button) findViewById(R.id.next);   
  73.         leftTime = (TextView) findViewById(R.id.lefttime);   
  74.         detail = (TextView) findViewById(R.id.detail);   
  75.         images = (ImageView) findViewById(R.id.images);   
  76.         price = (TextView) findViewById(R.id.price);   
  77.     }   
  78. }  
package jftt.txlong;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class GroupPurchase extends Activity {
	private Spinner changeCity;
	private Button refresh, pre, next;
	private TextView leftTime, detail, price, citygp;
	private ImageView images;
	private ArrayAdapter adapter;
	private List allItems;
	private String[] citys = { "北京市", "上海市", "天津市", "福州市" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initView();
		
		allItems = new ArrayList();
		for (int i = 0; i < citys.length; i++) {
			allItems.add(citys[i]);
		}
		adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, allItems);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		changeCity.setAdapter(adapter);
		changeCity.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView arg0, View arg1,
							int arg2, long arg3) {
						citygp.setText(changeCity.getSelectedItem().toString() + "今天的团购");
						Log.i("info-----------", changeCity.getSelectedItem().toString());
					}

					@Override
					public void onNothingSelected(AdapterView arg0) {
					}
				});

		pre.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i("info-----------", "prefer button has pressed!!!");
			}
		});

		next.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i("info-----------", "next button has pressed!!!");
			}
		});
	}

	private void initView() {
		changeCity = (Spinner) findViewById(R.id.changeCity);
		citygp = (TextView)findViewById(R.id.citygp);
		refresh = (Button) findViewById(R.id.refresh);
		pre = (Button) findViewById(R.id.pre);
		next = (Button) findViewById(R.id.next);
		leftTime = (TextView) findViewById(R.id.lefttime);
		detail = (TextView) findViewById(R.id.detail);
		images = (ImageView) findViewById(R.id.images);
		price = (TextView) findViewById(R.id.price);
	}
}

 main.xml

Xml代码  
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.        
  6.     <RelativeLayout android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content">  
  8.         <Spinner android:id="@+id/changeCity"    
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"    
  11.             android:layout_alignParentLeft="true"/>  
  12.         <TextView android:text="郑州今日团购"    
  13.             android:id="@+id/citygp"  
  14.             android:layout_width="wrap_content"    
  15.             android:layout_height="wrap_content"  
  16.             android:layout_toLeftOf="@+id/refresh"/>  
  17.         <Button android:text="刷新"  
  18.             android:id="@+id/refresh"  
  19.             android:layout_width="wrap_content"    
  20.             android:layout_height="wrap_content"    
  21.             android:layout_alignParentRight="true"/>  
  22.     RelativeLayout>  
  23.        
  24.     <RelativeLayout android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content">  
  26.         <TextView android:text="剩余时间:"    
  27.             android:layout_width="wrap_content"    
  28.             android:layout_height="wrap_content"    
  29.             android:layout_alignParentLeft="true"/>  
  30.         <TextView android:id="@+id/lefttime"    
  31.             android:layout_width="wrap_content"    
  32.             android:layout_height="wrap_content"    
  33.             android:layout_alignParentTop="true"    
  34.             android:layout_centerHorizontal="true"/>  
  35.         <Button android:text="订购"    
  36.             android:id="@+id/order"  
  37.             android:layout_width="wrap_content"    
  38.             android:layout_height="wrap_content"    
  39.             android:layout_alignParentRight="true"/>  
  40.     RelativeLayout>  
  41.        
  42.     <TextView android:id="@+id/detail"  
  43.         android:layout_width="fill_parent"  
  44.         android:layout_height="wrap_content"/>  
  45.     <LinearLayout android:layout_width="fill_parent"  
  46.         android:layout_height="wrap_content">  
  47.         <ImageView android:id="@+id/images"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"/>  
  50.         <TextView android:id="@+id/price"  
  51.             android:layout_width="wrap_content"  
  52.             android:layout_height="wrap_content"/>  
  53.     LinearLayout>  
  54.        
  55.     <RelativeLayout android:layout_width="fill_parent"  
  56.         android:layout_height="wrap_content">  
  57.         <Button android:id="@+id/pre"  
  58.             android:text="上一个"  
  59.             android:layout_width="wrap_content"  
  60.             android:layout_height="wrap_content"  
  61.             android:layout_alignParentLeft="true"/>  
  62.         <Button android:id="@+id/next"  
  63.             android:text="下一个"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.             android:layout_alignParentRight="true"/>  
  67.     RelativeLayout>  
  68.        
  69. LinearLayout>  
 

你可能感兴趣的:(Android)