日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android ListView适配器之SimpleAdapter的用法

發布時間:2024/10/8 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android ListView适配器之SimpleAdapter的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ?以前寫過ListView的適配器中最簡單的ArrayAdapter這個適配器,當listView中只需要顯示一個數據時,使用ArrayAdapter適配器很方便,但是如果要向listview的每一行顯示多行數據時,ArrayAdapter就不能滿足需求了。這個時候SimpleAdapter就派上用場了,SimpleAdapter可以讓ListView的每一行顯示多項數據,圖文并茂等。

? ? ? ?使用simpleAdapter的數據用一般都是HashMap構成的List,list的每一節對應ListView的每一行。HashMap的每個鍵值數據映射到布局文件中對應id的組件上。

構造函數:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

參數:context  SimpleAdapter關聯的View的運行環境

data    一個Map組成的List。在列表中的每個條目對應列表中的一行,每一個map中應該包含所有在from參數中指定的鍵

resource ? ?一個定義列表項的布局文件的資源ID。布局文件將至少應包含那些在to中定義了的ID

from ? ? ? ? ?一個將被添加到Map映射上的鍵名

to     將綁定數據的視圖的ID,跟from參數對

下面就舉個例子,要顯示在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"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兩項數據,首先要準備question和answer兩個數組,然后通過map將他們一一對應的添加到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[]{"日期:","姓名:","性別:","年齡:","聯系電話:"};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);} }完成的效果。


如果要顯示圖片和文字的話,就需要準備一系列的圖片文件,然后修改item,在item中添加一個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>實現類

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[]{"日期:","姓名:","性別:","年齡:","聯系電話:"};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);} }沒有去找圖片就用android機器人代替了


這樣就大功告成了~~~

總結

以上是生活随笔為你收集整理的android ListView适配器之SimpleAdapter的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。