Adapter简介 SimpleAdapter

来源:互联网 发布:oa免费办公软件 编辑:程序博客网 时间:2024/06/06 05:24

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MusicHomeActivity extends ListActivity
{
 @SuppressWarnings("unchecked")
 private ArrayList<Map<String, Object> >list = null;
 @SuppressWarnings("unchecked")
 private HashMap map = null;
 private LayoutInflater inflater = null;

 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);

  list = (ArrayList<Map<String, Object>>) getData();
  
  SimpleAdapter ad = new SimpleAdapter(this,list, R.layout.home
                          ,new String[]{"name","size","song","img"}
                                ,new int[]{R.id.t1,R.id.t2,R.id.t3,R.id.img});
  setListAdapter(ad);
  
  

 }
 
 public void onListItemClick(ListView l, View v, int position, long id)
 {
   HashMap map = (HashMap) list.get(position);
      Toast.makeText(this, "你点击了" +position+" "+map.get("name"), Toast.LENGTH_SHORT).show();     
 }

   @SuppressWarnings("unchecked")
 public List<Map<String, Object>> getData()
 {
  
  ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

  map = new HashMap<String, Object>();
  map.put("name", "黄昏");
  map.put("size", "5M");
  map.put("song", "小刚");
  map.put("img", R.drawable.icon);
  list.add(map);

  map = new HashMap<String, Object>();
  map.put("name", "大海");
  map.put("size", "5M");
  map.put("song", "张雨生");
  map.put("img", R.drawable.icon);
  list.add(map);

  return list;
 }

}

 

home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    

    <TextView android:id="@+id/t1"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="wrap_content" />
       
    <TextView
        android:id="@+id/t2"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/t3"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="wrap_content" />
       
    <ImageView android:id="@+id/img"
        android:layout_width="48dip"
        android:layout_height="48dip" />
 
</LinearLayout>

原创粉丝点击