ArrayAdapter
layout/item_select.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"android:textColor="#0000ff"android:textSize="17sp"android:text="火星"/>
<Spinnerandroid:id="@+id/sp_dropdown"android:layout_width="match_parent"android:layout_height="19dp"android:spinnerMode="dropdown"/>
private Spinner sp_dropdown;private static final String[] starArray = {"金星", "水星", "木星","地球", "木星", "土星"};
ArrayAdapter<String> starArrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, starArray);sp_dropdown.setAdapter(starArrayAdapter);
SimpleAdapter
private static final int[] iconArray = {R.drawable.shuixing, R.drawable.tuxing,R.drawable.muxing, R.drawable.diqiu,R.drawable.huoxing, R.drawable.jinxing,};private static final String[] starArray = {"金星", "水星", "木星", "地球", "木星", "土星"};
List<Map<String, Object>> list = new ArrayList<>();for (int i = 0; i < iconArray.length; i++) {Map<String, Object> item = new HashMap<>();item.put("icon", iconArray[i]);item.put("name", starArray[i]);list.add(item);}SimpleAdapter starSimpleAdapter = new SimpleAdapter(this, list,R.layout.item_simple,new String[]{"icon", "name"},new int[]{R.id.iv_icon, R.id.tv_name});sp_dropdown.setAdapter(starSimpleAdapter);sp_dropdown.setSelection(0);sp_dropdown.setOnItemSelectedListener(this);
案例代码
BaseAdapter
package com.example.study_android.adapter;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import com.example.study_android.R;
import com.example.study_android.bean.Planet;import java.util.List;public class PlaneBaseAdapter extends BaseAdapter {private Context mContext;private List<Planet> mPlaneList;public PlaneBaseAdapter(Context mContext, List<Planet> mPlaneList) {this.mContext = mContext;this.mPlaneList = mPlaneList;}@Overridepublic int getCount() {return mPlaneList.size();}@Overridepublic Object getItem(int i) {return mPlaneList.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View convertView, ViewGroup viewGroup) {View view = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);ImageView iv_icon = view.findViewById(R.id.iv_icon);TextView tv_name = view.findViewById(R.id.tv_name);TextView tv_desc = view.findViewById(R.id.tv_desc);Planet planet=mPlaneList.get(i);iv_icon.setImageResource(planet.image);tv_name.setText(planet.name);tv_desc.setText(planet.desc);return view;}
}
模拟数据
package com.example.study_android.bean;import com.example.study_android.R;import java.util.ArrayList;
import java.util.List;public class Planet {public int image;public String name;public String desc;public Planet(int image, String name, String desc) {this.image = image;this.name = name;this.desc = desc;}private static int[] iconArray = {R.drawable.shuixing,R.drawable.diqiu,R.drawable.shuixing,R.drawable.shuixing,R.drawable.shuixing,R.drawable.shuixing,};private static String[] nameArray = {"水星","水星","水星","水星","水星","水星",};private static String[] descArray = {"时间法律手段会计法李会计阿萨德","时间法律手段会计法李会计阿萨德","时间法律手段会计法李会计阿萨德","时间法律手段会计法李会计阿萨德","时间法律手段会计法李会计阿萨德","时间法律手段会计法李会计阿萨德",};public static List<Planet> getDefaultList() {List<Planet> planetList = new ArrayList<>();for (int i = 0; i < iconArray.length; i++) {planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));}return planetList;}
}
使用
Spinner sp_planet = findViewById(R.id.sp_planet);planetList = Planet.getDefaultList();PlaneBaseAdapter adapter = new PlaneBaseAdapter(this, planetList);sp_planet.setAdapter(adapter);sp_planet.setSelection(0);sp_planet.setOnItemSelectedListener(this);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv_icon"android:layout_width="0dp"android:layout_height="80dp"android:layout_weight="1"android:scaleType="fitCenter"android:src="@drawable/diqiu" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_marginLeft="5dp"android:layout_weight="3"android:orientation="vertical"><TextViewandroid:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="start|center"android:text="地球"android:textColor="@color/black"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_desc"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:gravity="start|center"android:text="地球是太阳系八大行星之一"android:textColor="@color/black"android:textSize="13sp" /></LinearLayout></LinearLayout>
案例代码