android ListView布局之二(是用simpleAdapter綁定資料)

來源:互聯網
上載者:User

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"?>  <!-- 建立存放一行資料的組件 -->  <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);          /* 參數一多,有些人就頭暈了。這裡解說下,各個參數的意思。          * 第一個參數 this 代表的是當前上下文,可以理解為你當前所處的activity          * 第二個參數 getData() 一個包含了資料的List,注意這個List裡存放的必須是map對象。simpleAdapter中的限制是這樣的List<? extends Map<String, ?>> data          * 第三個參數 R.layout.user 展示資訊的組件          * 第四個參數 一個string數組,數組記憶體放的是你存放資料的map裡面的key。          * 第五個參數:一個int數組,數組記憶體放的是你展示資訊組件中,每個資料的具體展示位置,與第四個參數一一對應          * */          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 準備一些測試資料      * @return 一個包含了資料資訊的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>  

廢話連綿:

我們一起看看結構,一個主布局檔案,一個組件布局檔案,一個Activity類。

依舊分為三步:

第一步:定義布局檔案,設計UI,包括尋找合適的圖片了等等……

第二步:擷取資料。這裡用的是simpleAdapter,所以要求資料必須固定格式的

第三步:綁定資料來源

然後,我們就可以看到我們想要的結果了。

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.