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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

android ListView布局之二(是用simpleAdapter绑定数据)

發(fā)布時間:2025/1/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android ListView布局之二(是用simpleAdapter绑定数据) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

main.xml主布局文件,代碼

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:text="@string/name" android:gravity="center" android:layout_width="150px" android:layout_height="wrap_content" /> <TextView android:text="@string/age" android:gravity="center" android:layout_width="170px" android:layout_height="wrap_content" /> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

  user.xml組件布局文件代碼

<?xml version="1.0" encoding="utf-8"?> <!-- 創(chuàng)建存放一行數(shù)據(jù)的組件 --> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow> <ImageView android:id="@+id/image" android:layout_width="50px" android:layout_height="50px" ></ImageView> <TextView android:id="@+id/userName" android:gravity="center" android:layout_height="wrap_content" android:layout_width="150px" ></TextView> <TextView android:id="@+id/userAge" android:gravity="center" android:layout_height="wrap_content" android:layout_width="170px" ></TextView> </TableRow> </TableLayout>

主Activity,listView.java代碼

package cn.com.android.listView; 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 listView extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listView = (ListView) findViewById(R.id.listView); /* 參數(shù)一多,有些人就頭暈了。這里解說下,各個參數(shù)的意思。 * 第一個參數(shù) this 代表的是當前上下文,可以理解為你當前所處的activity * 第二個參數(shù) getData() 一個包含了數(shù)據(jù)的List,注意這個List里存放的必須是map對象。simpleAdapter中的限制是這樣的List<? extends Map<String, ?>> data * 第三個參數(shù) R.layout.user 展示信息的組件 * 第四個參數(shù) 一個string數(shù)組,數(shù)組內(nèi)存放的是你存放數(shù)據(jù)的map里面的key。 * 第五個參數(shù):一個int數(shù)組,數(shù)組內(nèi)存放的是你展示信息組件中,每個數(shù)據(jù)的具體展示位置,與第四個參數(shù)一一對應 * */ SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.user, new String[]{"image","userName","userAge"}, new int[]{R.id.image,R.id.userName,R.id.userAge}); listView.setAdapter(adapter); } /** * @author chenzheng_java * @description 準備一些測試數(shù)據(jù) * @return 一個包含了數(shù)據(jù)信息的hashMap集合 */ private ArrayList<HashMap<String, Object>> getData(){ ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>(); for(int i=0;i<10;i++){ HashMap<String, Object> tempHashMap = new HashMap<String, Object>(); tempHashMap.put("image", R.drawable.icon); tempHashMap.put("userName", "用戶"+i); tempHashMap.put("userAge", 30-i); arrayList.add(tempHashMap); } return arrayList; } }

strings.xml代碼

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">布局列表展示</string> <string name="app_name">列表布局</string> <string name="name">姓名</string> <string name="age">年齡</string> </resources>

廢話連綿:

我們一起看看結(jié)構(gòu),一個主布局文件,一個組件布局文件,一個Activity類。

依舊分為三步:

第一步:定義布局文件,設(shè)計UI,包括尋找合適的圖片了等等……

第二步:獲取數(shù)據(jù)。這里用的是simpleAdapter,所以要求數(shù)據(jù)必須固定格式的

第三步:綁定數(shù)據(jù)源

然后,我們就可以看到我們想要的結(jié)果了。

?

?

?

總結(jié)

以上是生活随笔為你收集整理的android ListView布局之二(是用simpleAdapter绑定数据)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。