1.首先在activity_main.xml中建立ListView控件
<?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"> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:dividerHeight="2px" android:divider="#0000ff"> </ListView> </LinearLayout>
2.新建一个list_item.xml 存放每一行数据需要的控件
<?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"> <ImageView //图片控件 android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView //文本控件1 android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView //文本控件2 android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
3.java代码
package com.example.bluehat.wangshibo_ex3_2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv=(ListView)findViewById(R.id.lv); //建立一个list数组对象data,data中存放的是Map对象 List<Map<String,Object>> data=new ArrayList<Map<String,Object>>(); //新建多个Map对象 Map<String,Object> map1=new HashMap<String, Object>(); //向Map对象中写入值,前面的是数据列名,后面是具体的数据 map1.put("name","name0"); map1.put("age","10"); map1.put("icon",R.mipmap.ic_launcher); Map<String,Object> map2=new HashMap<String, Object>(); map2.put("name","name1"); map2.put("age","11"); map2.put("icon",R.mipmap.ic_launcher); Map<String,Object> map3=new HashMap<String, Object>(); map3.put("name","name2"); map3.put("age","12"); map3.put("icon",R.mipmap.ic_launcher); Map<String,Object> map4=new HashMap<String, Object>(); map4.put("name","name3"); map4.put("age","13"); map4.put("icon",R.mipmap.ic_launcher); Map<String,Object> map5=new HashMap<String, Object>(); map5.put("name","name4"); map5.put("age","14"); map5.put("icon",R.mipmap.ic_launcher); //将map对象添加到arrayList数组data中 data.add(map1); data.add(map2); data.add(map3); data.add(map4); data.add(map5); //为listview装配适配器 lv.setAdapter(new SimpleAdapter(this,data,R.layout.list_item,new String[]{"name","age","icon"},new int[]{R.id.tv1, R.id.tv2,R.id.iv})); //通过R.layout.list_item这个参数找到list_item,然后把data里面的数据显示在它上面各自对应的控件上,再一起作为一行在listview上显示出来 } // 至于为什么图片在左边显示,两行文本在右边则取决于list_item.xml }
4.效果显示
总结:
上面java代码中的 name age icon 可以理解为数据库表中的列名。map.put()要做的就是把数据插入到列中去。那么每一列可以有很多个数据,这就是为什么有多个map对象。
对象 | name | age | icon |
map1 | name0 | 10 | R.xxx |
map2 | name1 | 11 | R.xxx |
map3 | name2 | 12 | R.xxx |
map4 | name3 | 13 | R.xxx |
如果想使listview的一行显示更多数据,道理也一样。 相当于再增加一列数据就好了。