在前面的两篇文章中,我们讲到了关于ArrayAdapter的使用。用ArrayAdapter来在ListView中展示数据是很不错的,但是很多时候,我们的ListView中,可不只是展示文字,我们还想展示图片呢。
可能有些朋友刚才会问,第二篇不是已经可以展示图片了吗?是的呀,但是它就只能展示我们在xml中定义给它的那一张啊。
而究其原因,其实是因为我们传给它的数据源就只有字符串,没有传给图片给它,而事实上,我们也只能传文字给它,因为它用的是TextView嘛。
所以,光用ArrayAdapter显然不够丰富多彩,生活呀,总得有点不一样吧。
接下来我们就来看看在Android中关于SimpleAdapter的使用吧。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView1" android:layout_width="80dip" android:layout_height="60dip" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:contentDescription="testing"/> <TextView android:id="@+id/tvTitle" android:layout_width="80dip" android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1" android:layout_alignTop="@+id/imageView1"/> <TextView android:id="@+id/tvContent" android:layout_width="80dip" android:layout_height="wrap_content" android:layout_below="@+id/tvTitle" android:layout_toRightOf="@+id/imageView1" android:layout_alignBottom="@+id/imageView1"/> </RelativeLayout>我们定义了1个ImageView控件和两个TextView控件。
List<Map<String, Object>> simpleList = new ArrayList<Map<String, Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initSimpleList(); ListView listView = (ListView)findViewById(R.id.listView1); SimpleAdapter adapter = new SimpleAdapter(this, simpleList, R.layout.simpleadapter, new String[] {"title","content","drawable"}, new int[] {R.id.tvTitle,R.id.tvContent,R.id.imageView1}); listView.setAdapter(adapter); } private void initSimpleList(){ String[] titles = {"title1", "title2", "title3","title4", "title5", "title6"}; String[] contents = {"content1", "content2", "content3","content4", "content5", "content6"}; int[] drawableIds = {R.drawable.computer,R.drawable.excel,R.drawable.game, R.drawable.gc,R.drawable.network,R.drawable.pdf}; Map<String, Object> map; for(int i=0;i<6;i++){ map = new HashMap<String, Object>(); map.put("title", titles[i]); map.put("content", contents[i]); map.put("drawable", drawableIds[i]); simpleList.add(map); } }
/** * Constructor * * @param context The context where the View associated with this SimpleAdapter is running * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The * Maps contain the data for each row, and should include all the entries specified in * "from" * @param resource Resource identifier of a view layout that defines the views for this list * item. The layout file should include at least those named views defined in "to" * @param from A list of column names that will be added to the Map associated with each * item. * @param to The views that should display column in the "from" parameter. These should all be * TextViews. The first N views in this list are given the values of the first N columns * in the from parameter. */ public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { mData = data; mResource = mDropDownResource = resource; mFrom = from; mTo = to; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }1)context,上下文