Android软件开发之ListView 详解 (十一)

Android软件开发之ListView 详解




雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原文地址:http://blog.csdn.net/xys289187120/article/details/6636139



ListView的使用方法


ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 绘制出漂亮的列表,说道ListView就不得不说Adapter适配器,因为只有通过Adapter才可以把列表中的数据映射到ListView中。
在android的开发中最Adapter 一共可以分为
ArrayAdapter<T>,
BaseAdapter,
CursorAdapter,
HeaderViewListAdapter,
ResourceCursorAdapter,
SimpleAdapter,
SimpleCursorAdapter,
WrapperListAdapter

软件开发中最常用的有ArrayAdapter<T>, BaseAdapter, SimpleAdapter,今天我用一段代码向大家诠释如何使用ListView控件。



Android软件开发之ListView 详解 (十一)_第1张图片


1.简单的ListView

       在List列表中如果不存在过于复杂的东西 我们可以直接去new ArrayAdapter() 来绘制列表,无须继承ArrayAdapter,重写它的方法。但是如果列表中过于复杂的话就需要使用自定义布局来实现List列表。



Android软件开发之ListView 详解 (十一)_第2张图片



view plain copy to clipboard print ?
  1. public class SimpleList extends ListActivity { 
  2.     private String[] mListStr = {"姓名:雨松MOMO","性别:男","年龄:25","居住地:北京","邮箱:[email protected]"}; 
  3.     ListView mListView = null
  4.     @Override 
  5.     protected void onCreate(Bundle savedInstanceState) { 
  6.     mListView = getListView(); 
  7.     setListAdapter(new ArrayAdapter<String>(this
  8.                 android.R.layout.simple_list_item_1, mListStr)); 
  9.     mListView.setOnItemClickListener(new OnItemClickListener() { 
  10.         @Override 
  11.         public void onItemClick(AdapterView<?> adapterView, View view, int position, 
  12.             long id) { 
  13.         Toast.makeText(SimpleList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show(); 
  14.         } 
  15.     }); 
  16.  
  17.     super.onCreate(savedInstanceState); 
  18.     } 
public class SimpleList extends ListActivity { private String[] mListStr = {"姓名:雨松MOMO","性别:男","年龄:25","居住地:北京","邮箱:[email protected]"}; ListView mListView = null; @Override protected void onCreate(Bundle savedInstanceState) { mListView = getListView(); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListStr)); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(SimpleList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show(); } }); super.onCreate(savedInstanceState); } }


2.带标题的ListView列表

      使用 simpleAdapter 需要注意的是须要用Map<String,Object> item 来保存列表中每一项的显示的title与text , new SimpleAdapter的时候将map中的数据写入 ,程序就会帮我们绘制列表了。

Android软件开发之ListView 详解 (十一)_第3张图片



view plain copy to clipboard print ?
  1. public class TitleList extends ListActivity { 
  2.     private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; 
  3.     private String[] mListStr = { "雨松MOMO", "男", "25", "北京"
  4.         "[email protected]" }; 
  5.     ListView mListView = null
  6.     ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();; 
  7.  
  8.     @Override 
  9.     protected void onCreate(Bundle savedInstanceState) { 
  10.     mListView = getListView(); 
  11.        
  12.     int lengh = mListTitle.length; 
  13.     for(int i =0; i < lengh; i++) { 
  14.         Map<String,Object> item = new HashMap<String,Object>(); 
  15.         item.put("title", mListTitle[i]); 
  16.         item.put("text", mListStr[i]); 
  17.         mData.add(item);  
  18.     } 
  19.     SimpleAdapter adapter = new SimpleAdapter(this,mData,android.R.layout.simple_list_item_2, 
  20.         new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2}); 
  21.         setListAdapter(adapter); 
  22.     mListView.setOnItemClickListener(new OnItemClickListener() { 
  23.         @Override 
  24.         public void onItemClick(AdapterView<?> adapterView, View view, int position, 
  25.             long id) { 
  26.         Toast.makeText(TitleList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show(); 
  27.         } 
  28.     }); 
  29.     super.onCreate(savedInstanceState); 
  30.     } 
public class TitleList extends ListActivity { private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; private String[] mListStr = { "雨松MOMO", "男", "25", "北京", "[email protected]" }; ListView mListView = null; ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();; @Override protected void onCreate(Bundle savedInstanceState) { mListView = getListView(); int lengh = mListTitle.length; for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("title", mListTitle[i]); item.put("text", mListStr[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,android.R.layout.simple_list_item_2, new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2}); setListAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(TitleList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show(); } }); super.onCreate(savedInstanceState); } }
3.带图片的ListView列表

        使用 simpleAdapter 来操作 但是构造simpleAdapter的时候须要使用我们自己写的布局来完成 ,因为系统的布局已经不能满足需求了,同样Map<String,Object> item 来保存列表中每一项须要的显示内容 如 图片 标题 内容等



Android软件开发之ListView 详解 (十一)_第4张图片



添加我们自己的列表布局 图片 标题 内容
view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"> 
  5.     <ImageView android:id="@+id/image" 
  6.         android:layout_width="wrap_content" android:layout_height="fill_parent" 
  7.         android:layout_alignParentTop="true" android:layout_alignParentBottom="true" 
  8.         android:adjustViewBounds="true" 
  9.         android:padding="2dip" /> 
  10.     <TextView android:id="@+id/title" 
  11.         android:layout_width="wrap_content" android:layout_height="wrap_content" 
  12.         android:layout_toRightOf="@+id/image" 
  13.         android:layout_alignParentRight="true" android:layout_alignParentTop="true" 
  14.         android:layout_above="@+id/text" 
  15.         android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical" 
  16.         android:textSize="15dip" /> 
  17.     <TextView android:id="@+id/text" 
  18.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  19.         android:layout_toRightOf="@+id/image" 
  20.         android:layout_alignParentBottom="true" 
  21.         android:layout_alignParentRight="true" android:singleLine="true" 
  22.         android:ellipsize="marquee"  
  23.         android:textSize="20dip" /> 
  24. </RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:padding="2dip" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_above="@+id/text" android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical" android:textSize="15dip" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="20dip" /> </RelativeLayout>

view plain copy to clipboard print ?
  1. public class IconList extends ListActivity { 
  2.     private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; 
  3.     private String[] mListStr = { "雨松MOMO", "男", "25", "北京"
  4.         "[email protected]" }; 
  5.     ListView mListView = null
  6.     ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();; 
  7.  
  8.     @Override 
  9.     protected void onCreate(Bundle savedInstanceState) { 
  10.     mListView = getListView(); 
  11.        
  12.     int lengh = mListTitle.length; 
  13.     for(int i =0; i < lengh; i++) { 
  14.         Map<String,Object> item = new HashMap<String,Object>(); 
  15.         item.put("image", R.drawable.jay); 
  16.         item.put("title", mListTitle[i]); 
  17.         item.put("text", mListStr[i]); 
  18.         mData.add(item);  
  19.     } 
  20.     SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist, 
  21.         new String[]{"image","title","text"},new int[]{R.id.image,R.id.title,R.id.text}); 
  22.         setListAdapter(adapter); 
  23.     mListView.setOnItemClickListener(new OnItemClickListener() { 
  24.         @Override 
  25.         public void onItemClick(AdapterView<?> adapterView, View view, int position, 
  26.             long id) { 
  27.         Toast.makeText(IconList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show(); 
  28.         } 
  29.     }); 
  30.     super.onCreate(savedInstanceState); 
  31.     } 
public class IconList extends ListActivity { private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; private String[] mListStr = { "雨松MOMO", "男", "25", "北京", "[email protected]" }; ListView mListView = null; ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();; @Override protected void onCreate(Bundle savedInstanceState) { mListView = getListView(); int lengh = mListTitle.length; for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("image", R.drawable.jay); item.put("title", mListTitle[i]); item.put("text", mListStr[i]); mData.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist, new String[]{"image","title","text"},new int[]{R.id.image,R.id.title,R.id.text}); setListAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(IconList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show(); } }); super.onCreate(savedInstanceState); } }
4.自定义布局BaseAdapter修改列表颜色

       因为通过直接 构造系统的布局来绘制列表方法肯定是有限的,所以我们需要重写绘制方法 ,写一个类去继承BaseAdapter 并实现这个类中的方法,listView在一开始绘制的时候首先会调用getCout()方法得到绘制次数 ,然后会实例化自己定义的BaseAdapter通过getView()方法一层一层绘制ListView,所以我们可以在这里面根据position(当前绘制的ID)来任意的修改绘制的内容,做出好看漂亮的ListView,下面这个例子我通过重写getView修改每个列表的颜色 并且实现用户选中后成高亮状态。

Android软件开发之ListView 详解 (十一)_第5张图片

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content"> 
  5.     <ImageView android:id="@+id/color_image" 
  6.         android:layout_width="wrap_content" android:layout_height="fill_parent" 
  7.         android:layout_alignParentTop="true" android:layout_alignParentBottom="true" 
  8.         android:adjustViewBounds="true" 
  9.         android:padding="2dip" /> 
  10.     <TextView android:id="@+id/color_title" 
  11.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  12.         android:layout_toRightOf="@+id/color_image" 
  13.         android:layout_alignParentBottom="true" 
  14.         android:layout_alignParentRight="true" android:singleLine="true" 
  15.         android:ellipsize="marquee"  
  16.         android:textSize="15dip"  /> 
  17.     <TextView android:id="@+id/color_text" 
  18.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  19.         android:layout_toRightOf="@+id/color_image" 
  20.         android:layout_below="@+id/color_title" 
  21.         android:layout_alignParentBottom="true" 
  22.         android:layout_alignParentRight="true"  
  23.         android:singleLine="true" 
  24.         android:ellipsize="marquee"  
  25.         android:textSize="20dip" /> 
  26. </RelativeLayout> 
  27.  
  28.   
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/color_image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:padding="2dip" /> <TextView android:id="@+id/color_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/color_image" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="15dip" /> <TextView android:id="@+id/color_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/color_image" android:layout_below="@+id/color_title" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="20dip" /> </RelativeLayout>

view plain copy to clipboard print ?
  1. public class ColorList extends ListActivity { 
  2.     private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; 
  3.     private String[] mListStr = { "雨松MOMO", "男", "25", "北京"
  4.         "[email protected]" }; 
  5.     ListView mListView = null
  6.     MyListAdapter myAdapter = null
  7.     @Override 
  8.     protected void onCreate(Bundle savedInstanceState) { 
  9.     mListView = getListView(); 
  10.     myAdapter = new MyListAdapter(this); 
  11.     setListAdapter(myAdapter); 
  12.     mListView.setOnItemClickListener(new OnItemClickListener() { 
  13.         @Override 
  14.         public void onItemClick(AdapterView<?> adapterView, View view, int position, 
  15.             long id) { 
  16.         View v=adapterView.getChildAt(position); 
  17.         v.setBackgroundColor(Color.RED); 
  18.         Toast.makeText(ColorList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show(); 
  19.         } 
  20.     }); 
  21.      
  22.     super.onCreate(savedInstanceState); 
  23.     } 
  24.  
  25.     class MyListAdapter extends BaseAdapter { 
  26.     private int[] colors = new int[] { 0xff626569, 0xff4f5257 }; 
  27.     public MyListAdapter(Context context) { 
  28.         mContext = context; 
  29.     } 
  30.  
  31.     public int getCount() { 
  32.         return mListStr.length; 
  33.     } 
  34.  
  35.     @Override 
  36.     public boolean areAllItemsEnabled() { 
  37.         return false
  38.     } 
  39.  
  40.     public Object getItem(int position) { 
  41.         return position; 
  42.     } 
  43.  
  44.     public long getItemId(int position) { 
  45.         return position; 
  46.     } 
  47.  
  48.     public View getView(int position, View convertView, ViewGroup parent) { 
  49.         ImageView iamge = null
  50.         TextView title = null
  51.         TextView text = null
  52.         if (convertView == null) { 
  53.         convertView = LayoutInflater.from(mContext).inflate(R.layout.colorlist, null); 
  54.         iamge = (ImageView) convertView.findViewById(R.id.color_image); 
  55.         title =(TextView) convertView.findViewById(R.id.color_title); 
  56.         text= (TextView) convertView.findViewById(R.id.color_text); 
  57.         }  
  58.         int colorPos = position % colors.length; 
  59.         convertView.setBackgroundColor(colors[colorPos]); 
  60.         title.setText(mListTitle[position]); 
  61.         text.setText(mListStr[position]); 
  62.          iamge.setImageResource(R.drawable.jay); 
  63.         return convertView; 
  64.     } 
  65.  
  66.     private Context mContext; 
  67.     } 
public class ColorList extends ListActivity { private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; private String[] mListStr = { "雨松MOMO", "男", "25", "北京", "[email protected]" }; ListView mListView = null; MyListAdapter myAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { mListView = getListView(); myAdapter = new MyListAdapter(this); setListAdapter(myAdapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { View v=adapterView.getChildAt(position); v.setBackgroundColor(Color.RED); Toast.makeText(ColorList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show(); } }); super.onCreate(savedInstanceState); } class MyListAdapter extends BaseAdapter { private int[] colors = new int[] { 0xff626569, 0xff4f5257 }; public MyListAdapter(Context context) { mContext = context; } public int getCount() { return mListStr.length; } @Override public boolean areAllItemsEnabled() { return false; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView iamge = null; TextView title = null; TextView text = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.colorlist, null); iamge = (ImageView) convertView.findViewById(R.id.color_image); title =(TextView) convertView.findViewById(R.id.color_title); text= (TextView) convertView.findViewById(R.id.color_text); } int colorPos = position % colors.length; convertView.setBackgroundColor(colors[colorPos]); title.setText(mListTitle[position]); text.setText(mListStr[position]); iamge.setImageResource(R.drawable.jay); return convertView; } private Context mContext; } }


5.自定义布局ArrayAdapter
        ArrayAdapter是BaseAdapter的子类,ArrayAdapter不仅具有BaseAdapter的所有方法还自定义了一些新的方法来处理列表项,所以单纯的从功能能上来讲ArrayAdapter远远强与BaseAdapter,如果是绘制一些数量比较少的列表建议使用BaseAdapter 如果绘制一些比较复杂的列表项 并且列表项很多的 建议使用ArrayAdapter。




Android软件开发之ListView 详解 (十一)_第6张图片



view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content"> 
  5.     <Button 
  6.         android:id="@+id/array_button" 
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content" 
  9.         android:text="一个按钮" 
  10.     /> 
  11.     <ImageView android:id="@+id/array_image" 
  12.         android:layout_toRightOf="@+id/array_button" 
  13.         android:layout_width="wrap_content" android:layout_height="fill_parent" 
  14.         android:layout_alignParentTop="true" android:layout_alignParentBottom="true" 
  15.         android:adjustViewBounds="true" 
  16.         android:padding="2dip" /> 
  17.     <TextView android:id="@+id/array_title" 
  18.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  19.         android:layout_toRightOf="@+id/array_image" 
  20.         android:layout_alignParentBottom="true" 
  21.         android:layout_alignParentRight="true" android:singleLine="true" 
  22.         android:ellipsize="marquee"  
  23.         android:textSize="15dip"  /> 
  24.     <TextView android:id="@+id/array_text" 
  25.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  26.         android:layout_toRightOf="@+id/array_image" 
  27.         android:layout_below="@+id/array_title" 
  28.         android:layout_alignParentBottom="true" 
  29.         android:layout_alignParentRight="true"  
  30.         android:singleLine="true" 
  31.         android:ellipsize="marquee"  
  32.         android:textSize="20dip" /> 
  33.  
  34. </RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/array_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一个按钮" /> <ImageView android:id="@+id/array_image" android:layout_toRightOf="@+id/array_button" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:padding="2dip" /> <TextView android:id="@+id/array_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/array_image" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="15dip" /> <TextView android:id="@+id/array_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/array_image" android:layout_below="@+id/array_title" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:singleLine="true" android:ellipsize="marquee" android:textSize="20dip" /> </RelativeLayout>
view plain copy to clipboard print ?
  1. public class ArrayList extends ListActivity { 
  2.     private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"}; 
  3.     private String[] mListStr = { "雨松MOMO", "男", "25", "北京"
  4.         "[email protected]" }; 
  5.     ListView mListView = null
  6.     MyListAdapter myAdapter = null
  7.     ArrayList arrayList = null
  8.     @Override 
  9.     protected void onCreate(Bundle savedInstanceState) { 
  10.     arrayList = this
  11.     mListView = getListView(); 
  12.     myAdapter = new MyListAdapter(this,R.layout.arraylist); 
  13.     setListAdapter(myAdapter); 
  14.     super.onCreate(savedInstanceState); 
  15.     } 
  16.  
  17.     public class MyListAdapter extends ArrayAdapter<Object> { 
  18.     int mTextViewResourceID = 0
  19.     private Context mContext; 
  20.     public MyListAdapter(Context context, int textViewResourceId) { 
  21.         super(context, textViewResourceId); 
  22.         mTextViewResourceID = textViewResourceId; 
  23.         mContext = context; 
  24.     } 
  25.  
  26.     private int[] colors = new int[] { 0xff626569, 0xff4f5257 }; 
  27.  
  28.     public int getCount() { 
  29.         return mListStr.length; 
  30.     } 
  31.  
  32.     @Override 
  33.     public boolean areAllItemsEnabled() { 
  34.         return false
  35.     } 
  36.  
  37.     public Object getItem(int position) { 
  38.         return position; 
  39.     } 
  40.  
  41.     public long getItemId(int position) { 
  42.         return position; 
  43.     } 
  44.  
  45.     public View getView(final int position, View convertView, ViewGroup parent) { 
  46.         ImageView iamge = null
  47.         TextView title = null
  48.         TextView text = null
  49.         Button button = null
  50.         if (convertView == null) { 
  51.         convertView = LayoutInflater.from(mContext).inflate( 
  52.             mTextViewResourceID, null); 
  53.         iamge = (ImageView) convertView.findViewById(R.id.array_image); 
  54.         title = (TextView) convertView.findViewById(R.id.array_title); 
  55.         text = (TextView) convertView.findViewById(R.id.array_text); 
  56.         button = (Button)convertView.findViewById(R.id.array_button); 
  57.         button.setOnClickListener(new OnClickListener() { 
  58.              
  59.             @Override 
  60.             public void onClick(View arg0) { 
  61.             Toast.makeText(arrayList,"您点击的第"+position +"个按钮", Toast.LENGTH_LONG).show(); 
  62.              
  63.             } 
  64.         }); 
  65.         } 
  66.         int colorPos = position % colors.length; 
  67.         convertView.setBackgroundColor(colors[colorPos]); 
  68.         title.setText(mListTitle[position]); 
  69.         text.setText(mListStr[position]); 
  70.         if(colorPos == 0
  71.         iamge.setImageResource(R.drawable.jay); 
  72.         else 
  73.         iamge.setImageResource(R.drawable.image); 
  74.         return convertView; 
  75.     } 
  76.     } 

你可能感兴趣的:(android,String,object,ListView,layout,null)