1. 程式人生 > >Android學習--動態向SPinner控制元件中新增資料

Android學習--動態向SPinner控制元件中新增資料

android中的spinner動態載入資料:

GroupPurchase.java

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

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initView();
		
		allItems = new ArrayList<String>();
		for (int i = 0; i < citys.length; i++) {
			allItems.add(citys[i]);
		}
		adapter = new ArrayAdapter<String>(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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <RelativeLayoutandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content">
  7. <Spinnerandroid:id="@+id/changeCity"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"/>
  11. <TextViewandroid:text="鄭州今日團購"
  12. android:id="@+id/citygp"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_toLeftOf="@+id/refresh"/>
  16. <Buttonandroid:text="重新整理"
  17. android:id="@+id/refresh"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentRight="true"/>
  21. </RelativeLayout>
  22. <RelativeLayoutandroid:layout_width="fill_parent"
  23. android:layout_height="wrap_content">
  24. <TextViewandroid:text="剩餘時間:"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_alignParentLeft="true"/>
  28. <TextViewandroid:id="@+id/lefttime"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_alignParentTop="true"
  32. android:layout_centerHorizontal="true"/>
  33. <Buttonandroid:text="訂購"
  34. android:id="@+id/order"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_alignParentRight="true"/>
  38. </RelativeLayout>
  39. <TextViewandroid:id="@+id/detail"
  40. android:layout_width="fill_parent"
  41. android:layout_height="wrap_content"/>
  42. <LinearLayoutandroid:layout_width="fill_parent"
  43. android:layout_height="wrap_content">
  44. <ImageViewandroid:id="@+id/images"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"/>
  47. <TextViewandroid:id="@+id/price"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"/>
  50. </LinearLayout>
  51. <RelativeLayoutandroid:layout_width="fill_parent"
  52. android:layout_height="wrap_content">
  53. <Buttonandroid:id="@+id/pre"
  54. android:text="上一個"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_alignParentLeft="true"/>
  58. <Buttonandroid:id="@+id/next"
  59. android:text="下一個"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_alignParentRight="true"/>
  63. </RelativeLayout>
  64. </LinearLayout>