Android學習--動態向SPinner控制元件中新增資料
阿新 • • 發佈:2019-02-19
android中的spinner動態載入資料:
GroupPurchase.java
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;
- publicclass 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
- publicvoid 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
- publicvoid onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- citygp.setText(changeCity.getSelectedItem().toString() + "今天的團購");
- Log.i("info-----------", changeCity.getSelectedItem().toString());
- }
- @Override
- publicvoid onNothingSelected(AdapterView<?> arg0) {
- }
- });
- pre.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- Log.i("info-----------", "prefer button has pressed!!!");
- }
- });
- next.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- Log.i("info-----------", "next button has pressed!!!");
- }
- });
- }
- privatevoid 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程式碼- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <RelativeLayoutandroid:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Spinnerandroid:id="@+id/changeCity"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <TextViewandroid:text="鄭州今日團購"
- android:id="@+id/citygp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@+id/refresh"/>
- <Buttonandroid:text="重新整理"
- android:id="@+id/refresh"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"/>
- </RelativeLayout>
- <RelativeLayoutandroid:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextViewandroid:text="剩餘時間:"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <TextViewandroid:id="@+id/lefttime"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"/>
- <Buttonandroid:text="訂購"
- android:id="@+id/order"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"/>
- </RelativeLayout>
- <TextViewandroid:id="@+id/detail"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <LinearLayoutandroid:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <ImageViewandroid:id="@+id/images"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextViewandroid:id="@+id/price"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <RelativeLayoutandroid:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Buttonandroid:id="@+id/pre"
- android:text="上一個"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"/>
- <Buttonandroid:id="@+id/next"
- android:text="下一個"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"/>
- </RelativeLayout>
- </LinearLayout>