这个简单适配器呢支持两个控件 比如一个图片和一个文本区,本次例子就是以此为例
首先写好xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_marginLeft="20dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv_imageView"/>
<TextView
android:textSize="30sp"
android:layout_marginTop="40dp"
android:layout_marginLeft="60dp"
android:id="@+id/tv_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
其次写好简单数组适配器
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(int i = 0 ; i<icom.length;i++)
{
Map<String, Object> item= new HashMap<String,Object>();
item.put("icon",icom[i]);
item.put("name",starArry[i]);
list.add(item);
}
SimpleAdapter simpleAdapter = new SimpleAdapter
(this,list,R.layout.item_drowdown,new String[]{"icon","name"},
new int[]{R.id.iv_imageView,R.id.tv_textView});
再者找到载体Spinner
并设置简单数组
Spinner sp = findViewById(R.id.sp_spnner2);
sp.setPrompt("请选择行星");
sp.setAdapter(simpleAdapter);
sp.setSelection(0);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{Toast.makeText(MainActivity.this, "你选择的是" + starArry[position],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
这就大功告成了