Android 开发笔记___spinner__适配器基础_arrayadapter

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:padding="20dp" >
 6 
 7     <Spinner
 8         android:id="@+id/sp_dialog"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:spinnerMode="dialog" />
12 </LinearLayout>
1 <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
2     android:id="@+id/tv_name"
3     android:layout_width="match_parent"
4     android:layout_height="40dp"
5     android:singleLine="true"
6     android:gravity="center"
7     android:textSize="17sp"
8     android:textColor="#ff0000" />
 1 package com.example.alimjan.hello_world;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.view.View;
 8 import android.widget.AdapterView;
 9 import android.widget.ArrayAdapter;
10 import android.widget.Spinner;
11 import android.widget.Toast;
12 
13 /**
14  * Created by alimjan on 7/2/2017.
15  */
16 
17 public class class_3_3_1_1 extends AppCompatActivity {
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.code_3_3_1_1);
23 
24         ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,
25                 R.layout.item_select, starArray);
26         starAdapter.setDropDownViewResource(R.layout.item_dropdown);
27 
28         Spinner sp = (Spinner) findViewById(R.id.sp_dialog);
29         sp.setPrompt("请选择行星");
30         sp.setAdapter(starAdapter);
31         sp.setSelection(0);
32         sp.setOnItemSelectedListener(new MySelectedListener());
33     }
34 
35     private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
36     class MySelectedListener implements AdapterView.OnItemSelectedListener {
37         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
38             Toast.makeText(class_3_3_1_1.this, "您选择的是"+starArray[arg2], Toast.LENGTH_LONG).show();
39         }
40 
41         public void onNothingSelected(AdapterView<?> arg0) {
42         }
43     }
44     public static void startHome(Context mContext) {
45         Intent intent = new Intent(mContext, class_3_3_1_1.class);
46         mContext.startActivity(intent);
47     }
48 
49 }

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:padding="20dp" >
 6 
 7     <Spinner
 8         android:id="@+id/sp_icon"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:spinnerMode="dialog" />
12 
13 </LinearLayout>
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="wrap_content"
 4     android:orientation="horizontal" >
 5 
 6     <ImageView
 7         android:id="@+id/iv_icon"
 8         android:layout_width="0dp"
 9         android:layout_height="50dp"
10         android:layout_weight="1"
11         android:gravity="center" />
12     
13     <TextView
14         android:id="@+id/tv_name"
15         android:layout_width="0dp"
16         android:layout_height="match_parent"
17         android:layout_weight="3"
18         android:gravity="center"
19         android:textSize="17sp"
20         android:textColor="#ff0000" />
21 
22 </LinearLayout>
 1 package com.example.alimjan.hello_world;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.view.View;
 8 import android.widget.AdapterView;
 9 import android.widget.SimpleAdapter;
10 import android.widget.Spinner;
11 import android.widget.Toast;
12 
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 
18 /**
19  * Created by alimjan on 7/2/2017.
20  */
21 
22 public class class_3_3_1_2 extends AppCompatActivity {
23 
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.code_3_3_1_2);
28 
29         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
30         for (int i = 0; i < iconArray.length; i++) {
31             Map<String, Object> item = new HashMap<String, Object>();
32             item.put("icon", iconArray[i]);
33             item.put("name", starArray[i]);
34             list.add(item);
35         }
36         SimpleAdapter starAdapter = new SimpleAdapter(this, list,
37                 R.layout.item_select, new String[] { "icon", "name" },
38                 new int[] {R.id.iv_icon, R.id.tv_name});
39         starAdapter.setDropDownViewResource(R.layout.item_simple);
40 
41         Spinner sp = (Spinner) findViewById(R.id.sp_icon);
42         sp.setPrompt("请选择行星");
43         sp.setAdapter(starAdapter);
44         sp.setSelection(0);
45         sp.setOnItemSelectedListener(new MySelectedListener());
46     }
47 
48     private int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
49             R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
50     private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
51     class MySelectedListener implements AdapterView.OnItemSelectedListener {
52         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
53             Toast.makeText(class_3_3_1_2.this, "您选择的是"+starArray[arg2], Toast.LENGTH_LONG).show();
54         }
55 
56         public void onNothingSelected(AdapterView<?> arg0) {
57         }
58     }
59 
60     public static void startHome(Context mContext) {
61         Intent intent = new Intent(mContext, class_3_3_1_2.class);
62         mContext.startActivity(intent);
63     }
64 
65 }

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:padding="20dp" >
 6 
 7     <Spinner
 8         android:id="@+id/sp_dropdown"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:spinnerMode="dropdown" />
12 
13 </LinearLayout>
 1 package com.example.alimjan.hello_world;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.view.View;
 8 import android.widget.AdapterView;
 9 import android.widget.ArrayAdapter;
10 import android.widget.Spinner;
11 import android.widget.Toast;
12 
13 /**
14  * Created by alimjan on 7/2/2017.
15  */
16 
17 public class class_3_3_1_3 extends AppCompatActivity {
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.code_3_3_1_3);
23 
24         ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,
25                 R.layout.item_select, starArray);
26         starAdapter.setDropDownViewResource(R.layout.item_dropdown);
27 
28         Spinner sp = (Spinner) findViewById(R.id.sp_dropdown);
29         sp.setPrompt("请选择行星");
30         sp.setAdapter(starAdapter);
31         sp.setSelection(0);
32         sp.setOnItemSelectedListener(new MySelectedListener());
33     }
34 
35     private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
36     class MySelectedListener implements AdapterView.OnItemSelectedListener {
37         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
38             Toast.makeText(class_3_3_1_3.this, "您选择的是"+starArray[arg2], Toast.LENGTH_LONG).show();
39         }
40 
41         public void onNothingSelected(AdapterView<?> arg0) {
42         }
43     }
44     public static void startHome(Context mContext) {
45         Intent intent = new Intent(mContext, class_3_3_1_3.class);
46         mContext.startActivity(intent);
47     }
48 
49 }

 

posted @ 2017-07-02 16:37  alm  阅读(355)  评论(0编辑  收藏  举报