Android学习--动态向SPinner控件中添加数据
16lz
2021-01-23
android中的spinner动态加载内容
博客分类: Android Android OS XMLandroid中的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;
- 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);
- }
- }
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 = { "北京市", "上海市", "天津市", "福州市" };@Overridepublic 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() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {citygp.setText(changeCity.getSelectedItem().toString() + "今天的团购");Log.i("info-----------", changeCity.getSelectedItem().toString());}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}});pre.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.i("info-----------", "prefer button has pressed!!!");}});next.setOnClickListener(new View.OnClickListener() {@Overridepublic 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代码- <?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>
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer