国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 一起学android之SimpleAdapter使用(13)

一起学android之SimpleAdapter使用(13)

来源:程序员人生   发布时间:2015-01-06 08:07:42 阅读次数:2125次

SimpleAdapter:将List集合的多个对象包装成列表项。

我们可以通过SimpleAdapter来实现1些复杂的列表,请看以下实例:


simpleadapter_list布局文件:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_simpleadapter" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout></span>

simpleadapter布局文件:用于显示列表项的组件

<span style="font-size:18px;"><?xml version="1.0" encoding="utf⑻"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll_top" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/iv_image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="fitXY" android:src="@drawable/a5i" android:layout_gravity="center"/> <LinearLayout android:id="@+id/ll_right" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="4dp"> <TextView android:id="@+id/tv_title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:text="AS"/> <TextView android:id="@+id/tv_des" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="13sp" android:text="AS" /> </LinearLayout> </LinearLayout> </RelativeLayout></span>

SimpleAdapterTest主文件:

<span style="font-size:18px;">public class SimpleAdapterTest extends Activity { int[] images = new int[] { R.drawable.a5i, R.drawable.a5j, R.drawable.a5k }; String[] titles = new String[] { "电话", "QQ", "联系人" }; String[] des = new String[] { "当前无电话记录", "当前无QQ聊天记录", "最近无联系人" }; private ListView lv_simple; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simpleadapter_list); initView(); setData(); } private void initView() { lv_simple = (ListView) findViewById(R.id.lv_simpleadapter); lv_simple.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SimpleAdapterTest.this, titles[position], Toast.LENGTH_SHORT).show(); } }); } private void setData() { // 创建数据源 List<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < titles.length; i++) { // 每一个列表项的内容 HashMap<String, Object> listItem = new HashMap<String, Object>(); listItem.put("image", images[i]); listItem.put("title", titles[i]); listItem.put("desc", des[i]); listItems.add(listItem); } /* * SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) * 参数说明: * context:全部利用的上下文。 * data:是List<? extends Map<String, ?>>的集合对象,其中每一个Map<String, ?>代表每列的列表项内容。 * resource:界面布局文件的ID。 * from:String[]类型的参数,指定了Map<String, ?>中的每一个key对应的value来生成列表项。 * to:int[]类型的参数,显示每一个列表项显示的组件。 */ SimpleAdapter simpleAdapter = new SimpleAdapter(SimpleAdapterTest.this, listItems, R.layout.simpleadapter, new String[] { "image", "title", "desc" }, new int[] { R.id.iv_image, R.id.tv_title, R.id.tv_des }); //绑定适配器 lv_simple.setAdapter(simpleAdapter); } }</span>







转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/42361041  情绪控_







生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生