android ListView布局之二(是用simpleAdapter綁定數據)


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,所以要求數據必須固定格式的

第三步:綁定數據源

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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM