android ListView适配器之SimpleAdapter的用法
? ? ? ?以前寫(xiě)過(guò)ListView的適配器中最簡(jiǎn)單的ArrayAdapter這個(gè)適配器,當(dāng)listView中只需要顯示一個(gè)數(shù)據(jù)時(shí),使用ArrayAdapter適配器很方便,但是如果要向listview的每一行顯示多行數(shù)據(jù)時(shí),ArrayAdapter就不能滿足需求了。這個(gè)時(shí)候SimpleAdapter就派上用場(chǎng)了,SimpleAdapter可以讓ListView的每一行顯示多項(xiàng)數(shù)據(jù),圖文并茂等。
? ? ? ?使用simpleAdapter的數(shù)據(jù)用一般都是HashMap構(gòu)成的List,list的每一節(jié)對(duì)應(yīng)ListView的每一行。HashMap的每個(gè)鍵值數(shù)據(jù)映射到布局文件中對(duì)應(yīng)id的組件上。
構(gòu)造函數(shù):
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)參數(shù):context SimpleAdapter關(guān)聯(lián)的View的運(yùn)行環(huán)境
data 一個(gè)Map組成的List。在列表中的每個(gè)條目對(duì)應(yīng)列表中的一行,每一個(gè)map中應(yīng)該包含所有在from參數(shù)中指定的鍵
resource ? ?一個(gè)定義列表項(xiàng)的布局文件的資源ID。布局文件將至少應(yīng)包含那些在to中定義了的ID
from ? ? ? ? ?一個(gè)將被添加到Map映射上的鍵名
to 將綁定數(shù)據(jù)的視圖的ID,跟from參數(shù)對(duì)
下面就舉個(gè)例子,要顯示在listView的一行中顯示一個(gè)人的信息。
主布局文件
<?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:background="@drawable/background"android:orientation="vertical" ><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="30sp"android:textColor="#ffffff"android:text="病人一般信息"/><ListView android:id="@+id/lv01"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>listView的item
<?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" ><TextViewandroid:id="@+id/question"android:layout_width="0sp"android:layout_weight="1"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="25sp"/><TextViewandroid:id="@+id/answer"android:layout_width="0sp"android:layout_weight="1"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="25sp"/></LinearLayout>
在ListView的每一行中顯示question和answer兩項(xiàng)數(shù)據(jù),首先要準(zhǔn)備question和answer兩個(gè)數(shù)組,然后通過(guò)map將他們一一對(duì)應(yīng)的添加到liStView的每一行中去。
package com.example.project_simpleadapter;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter;public class MainActivity extends Activity {private ListView listView;private String [] question= new String[]{"日期:","姓名:","性別:","年齡:","聯(lián)系電話:"};private String [] answer= new String[]{"2015.4.30","張三","男","58","15254537894"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.lv01);List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;for(int i =0; i < answer.length; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("question", question[i]); item.put("answer", answer[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.item,new String[]{"question","answer"},new int[]{R.id.question,R.id.answer}); listView.setAdapter(adapter);} }完成的效果。
如果要顯示圖片和文字的話,就需要準(zhǔn)備一系列的圖片文件,然后修改item,在item中添加一個(gè)image
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"android:orientation="horizontal" ><ImageView android:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/question"android:layout_width="0sp"android:layout_weight="1"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="25sp"/><TextViewandroid:id="@+id/answer"android:layout_width="0sp"android:layout_weight="1"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="25sp"/></LinearLayout>實(shí)現(xiàn)類
package com.example.project_simpleadapter;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter;public class MainActivity extends Activity {private ListView listView;private int [] images = new int []{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};private String [] question= new String[]{"日期:","姓名:","性別:","年齡:","聯(lián)系電話:"};private String [] answer= new String[]{"2015.4.30","張三","男","58","15254537894"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.lv01);List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;for(int i =0; i < answer.length; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("images", images[i]);item.put("question", question[i]); item.put("answer", answer[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.item,new String[]{"images","question","answer"},new int[]{R.id.image,R.id.question,R.id.answer}); listView.setAdapter(adapter);} }沒(méi)有去找圖片就用android機(jī)器人代替了
這樣就大功告成了~~~
總結(jié)
以上是生活随笔為你收集整理的android ListView适配器之SimpleAdapter的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android 简单的音乐播放器
- 下一篇: 15款ix35内后视镜有根电线是干嘛的?