本文主要是介绍SimpleAdapter的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
步骤:
1.获取Spinner控件
2.获取数据源
List
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.yztc.day0703_wang_06.MainActivity"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="图书信息"android:id="@+id/tv"android:layout_centerInParent="true"android:layout_marginBottom="10dp"/><Spinner
android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/spinner"android:layout_below="@id/tv"android:layout_centerInParent="true"></Spinner>
</RelativeLayout>
列表中显式的子项布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:padding="10dp"><ImageView
android:id="@+id/image_view"android:layout_width="50dp"android:layout_height="50dp"/><TextView
android:id="@+id/name"android:layout_width="wrap_content"android:layout_height="50dp"android:layout_marginLeft="10dp"android:gravity="center"/></LinearLayout>
逻辑代码:
public class MainActivity extends AppCompatActivity {private Spinner mSpinner;private int[] images = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5};private List<Map<String, Object>> list = new ArrayList<>();private String[] name = new String[]{"秦琼", "罗成", "楚留香", "胡铁花", "姬冰雁"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSpinner = (Spinner) findViewById(R.id.spinner);for (int i = 0; i < images.length; i++) {Map<String, Object> map = new HashMap<>();map.put("icon", images[i]);map.put("name", name[i]);list.add(map);}SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item, new String[]{"icon", "name"}, new int[]{R.id.image_view, R.id.name});mSpinner.setAdapter(adapter);}
}
显式如下:
这篇关于SimpleAdapter的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!