Android Studio中Spinner控件的数据绑定实现

简介: 这里介绍使用集合在程序中为Spinner控件设定数据源,步骤如下:1、在Android Studio界面中,选择“Project”,然后展开"app"->"res"->"layout",打开activity_main.

这里介绍使用集合在程序中为Spinner控件设定数据源,步骤如下:

1、在Android Studio界面中,选择“Project”,然后展开"app"->"res"->"layout",打开activity_main.xml,添加一个Spinner控件,代码如下:

<Spinner
        android:id="@+id/eduSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
</Spinner>


2、找到“app”->"java"下的MainActivity.java文件,定义几个变量,代码如下:

 private List<CharSequence> eduList = null;
 private ArrayAdapter<CharSequence> eduAdapter = null;
 private Spinner eduSpinner= null;



3、在Activity的OnCreate方法后面,添加如下方法:

  //找到Spinner控件
  eduSpinner = (Spinner)super.findViewById(R.id.eduSpinner);
  eduSpinner.setPrompt("请选择您的学历:");
  eduList = new ArrayList<CharSequence>();
  eduList.add("大专");
  eduList.add("本科");
  eduList.add("硕士");
  eduList.add("其他");
  eduAdapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,eduList);
  eduAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  eduSpinner.setAdapter(eduAdapter);



4、运行程序,Spinner中已经绑定了对应的数据,如下图所示:



在后面该绑定的数据可以通过WebService从服务器上获取对应的数据,然后绑定到Spinner控件上。


===========================================================================

如果觉得对您有帮助,微信扫一扫支持一下:


相关文章
|
10天前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android&#39;s AsyncTask simplifies asynchronous tasks for brief background work, bridging UI and worker threads. It involves execute() for starting tasks, doInBackground() for background execution, publishProgress() for progress updates, and onPostExecute() for returning results to the main thread.
10 0
|
10天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
10 0
|
17天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
24天前
|
XML Java Android开发
Android每点击一次按钮就添加一条数据
Android每点击一次按钮就添加一条数据
24 1
|
27天前
|
Java Android开发
Android Studio的使用导入第三方Jar包
Android Studio的使用导入第三方Jar包
12 1
|
1月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
12 0
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
35 3
|
2月前
|
数据库 Android开发 数据库管理
【Android】使用android studio查看内置数据库信息
【Android】使用android studio查看内置数据库信息
74 0
|
2月前
|
编译器 开发工具 Android开发
|
2月前
|
JavaScript Java 数据安全/隐私保护
安卓逆向 -- POST数据解密
安卓逆向 -- POST数据解密
27 2