对于ArrayAdapter,里面虽然能添加图片,但只能是相同的图片。
废话不多说:
布局&&list的item布局
<LinearLayout 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=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?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" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
程序
private static String[] names= {"功能1","功能2","功能3","功能4","功能5"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.tv, names)); }
--------------------------简单的分割线--------------0-0-----------------------------------------------------------
SimpleAdapter可以添加图片的哈~~每一个item都可以加自己想要的图片。
在item布局里面给imageView添加id为iv。
程序
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv); List<Map<String,Object>> data = new ArrayList<Map<String,Object>>(); Map<String,Object> map1 = new HashMap<String, Object>(); map1.put("nameText", "第1个啦啦啦"); map1.put("icon", R.drawable.ic_launcher); Map<String,Object> map2 = new HashMap<String, Object>(); map2.put("nameText", "第2个啦啦啦"); map2.put("icon", R.drawable.info_buse); Map<String,Object> map3 = new HashMap<String, Object>(); map3.put("nameText", "第3个啦啦啦"); map3.put("icon", R.drawable.info_duijiao); Map<String,Object> map4 = new HashMap<String, Object>(); map4.put("nameText", "第4个啦啦啦"); map4.put("icon", R.drawable.info_flash); data.add(map1); data.add(map2); data.add(map3); data.add(map4); lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"nameText", "icon"},new int[]{R.id.tv,R.id.iv})); }
效果:
data为绑定的数据,list集合。R.layout.list_item,数据显示对应的布局,这是要让数据跟view对象奖励一个映射关系。from[] ,map集合里面的数据的key。to[],布局文件里面的id。
我是天王盖地虎的分割线
源代码:http://pan.baidu.com/s/1dD1Qx01
listview学习2.zip
转载请注明出处:http://www.cnblogs.com/yydcdut