ListView 的簡單使用day9

ListView 的簡單使用day9android

功能:把數據綁定到ListView控件上
//數據有圖片 姓名 年齡
//res/drawable-hdpi自行放入須要的圖片ide

一、在res/layout佈局界面有2個xml文件佈局

//activity_main.xml文件 佈局this

代碼xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >事件

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />圖片

</RelativeLayout>
------------------------get

//simplete_activity.xml文件 佈局it

代碼io

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"
        android:layout_alignTop="@+id/image"
        android:text="name"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_alignBottom="@+id/image"
        android:text="age"
        android:textSize="15sp" />

</RelativeLayout>

-----------------------

二、MainActivity 類

代碼

public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter;
//準備須要的數據
private String[] array_name = {"哈哈","呵呵","拉拉","嘻嘻","噠噠","嘎嘎","嗚嗚","喔喔","嘰嘰"};
private int[] array_age = {12,21,32,11,14,16,43,31,22};
private int[] array_images = {R.drawable.img01,R.drawable.img02,R.drawable.img03,
  R.drawable.img04,R.drawable.img05,R.drawable.img06,
  R.drawable.img07,R.drawable.img08,R.drawable.img09,};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.listview = (ListView) this.findViewById(R.id.listview);
  
  List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
 
//利用循環 把須要的數據添加到一個集合 
     for(int i = 0;i<array_name.length;i++){
      Map<String,Object> map = new HashMap<String, Object>();
      map.put("head", array_images[i]);
      map.put("name", array_name[i]);
      map.put("age", array_age[i]);
      
      data.add(map);
     }
//new一個自定義適配器
//找到自定義的佈局文件simplete_activity.xml文件
//而後把存入集合的數據丟入自定義適配器裏
  this.adapter = new SimpleAdapter(this, data, R.layout.simplete_activity,
  new String[]{"head","name","age"}, new int[]{R.id.image,R.id.name,R.id.age});
 
//把自定義適配器綁定到ListView
  listview.setAdapter(adapter);
//點擊ListView的事件 監聽
  listview.setOnItemClickListener(new OnItemClickListener() {

   @Override   public void onItemClick(AdapterView<?> parent, View view,     int position, long id) {    Map<String, Object> select_item = (Map<String, Object>) adapter.getItem(position);//把點擊到的 而後須要的數據設置到標題    setTitle(select_item.get("name").toString());       }  }); }}

相關文章
相關標籤/搜索