android中的spinner动态加载数据:
GroupPurchase.java
- 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);
- }
- }
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 version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Spinner android:id="@+id/changeCity"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <TextView android:text="郑州今日团购"
- android:id="@+id/citygp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@+id/refresh"/>
- <Button android:text="刷新"
- android:id="@+id/refresh"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"/>
- </RelativeLayout>
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView android:text="剩余时间:"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <TextView android:id="@+id/lefttime"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"/>
- <Button android:text="订购"
- android:id="@+id/order"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"/>
- </RelativeLayout>
- <TextView android:id="@+id/detail"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <LinearLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <ImageView android:id="@+id/images"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView android:id="@+id/price"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:id="@+id/pre"
- android:text="上一个"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <Button android:id="@+id/next"
- android:text="下一个"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"/>
- </RelativeLayout>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Spinner android:id="@+id/changeCity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"/> <TextView android:text="郑州今日团购" android:id="@+id/citygp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/refresh"/> <Button android:text="刷新" android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="剩余时间:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"/> <TextView android:id="@+id/lefttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/> <Button android:text="订购" android:id="@+id/order" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> <TextView android:id="@+id/detail" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/images" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/pre" android:text="上一个" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"/> <Button android:id="@+id/next" android:text="下一个" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> </LinearLayout>