使用自定义的BaseAdapter实现LIstView的展示(转)

使用自定义的BaseAdapter实现LIstView的展示

 

实现以下功能点:

 

1.通过自定义的BaseAdapter展示ListView;

2.动态修改、刷新ListView内容;

3.在自定义的BaseAdapter中实现ListView内容的点击和长按点击事件;

4.在BaseAdapter的getView方法中动态增加新组件;

 

 

 

效果图如下:

 


 

 

xml配置文件1(list_view01.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         >  
  12.         <EditText     
  13.             android:id="@+id/editText01Edit"  
  14.             android:layout_width="110sp"    
  15.             android:layout_height="wrap_content"    
  16.             android:text=""  
  17.             />  
  18.         <Button     
  19.             android:id="@+id/button01Edit"  
  20.             android:layout_width="wrap_content"    
  21.             android:layout_height="wrap_content"    
  22.             android:text="动态修改第1行第2列数据"  
  23.             android:onClick="editPersonAndRefreshListView01"  
  24.             />  
  25.     </LinearLayout>  
  26.     <ListView     
  27.         android:id="@+id/listView01"  
  28.         android:layout_width="fill_parent"    
  29.         android:layout_height="fill_parent"    
  30.         />  
  31. </LinearLayout>  
<?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"
    >
    <LinearLayout
    	android:orientation="horizontal"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	>
	    <EditText  
		    android:id="@+id/editText01Edit"
		    android:layout_width="110sp" 
		    android:layout_height="wrap_content" 
		    android:text=""
		    />
	    <Button  
		    android:id="@+id/button01Edit"
		    android:layout_width="wrap_content" 
		    android:layout_height="wrap_content" 
		    android:text="动态修改第1行第2列数据"
		    android:onClick="editPersonAndRefreshListView01"
		    />
	</LinearLayout>
	<ListView  
	    android:id="@+id/listView01"
	    android:layout_width="fill_parent" 
	    android:layout_height="fill_parent" 
	    />
</LinearLayout>
 

 

xml配置文件2(list_view01_item.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView     
  8.     android:id="@+id/listView01Item01"  
  9.     android:layout_width="50sp"    
  10.     android:layout_height="wrap_content"    
  11.     />  
  12. <TextView     
  13.     android:id="@+id/listView01Item02"  
  14.     android:layout_width="90sp"    
  15.     android:layout_height="wrap_content"    
  16.     />    
  17. <TextView     
  18.     android:id="@+id/listView01Item03"  
  19.     android:layout_width="wrap_content"    
  20.     android:layout_height="wrap_content"    
  21.     />       
  22. </LinearLayout>  
<?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/listView01Item01"
    android:layout_width="50sp" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:id="@+id/listView01Item02"
    android:layout_width="90sp" 
    android:layout_height="wrap_content" 
    /> 
<TextView  
    android:id="@+id/listView01Item03"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />    
</LinearLayout>
 

Activity类代码:

Java代码 复制代码 收藏代码
  1. package com.zhouzijing.android;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import com.zhouzijing.android.util.MenuEntity;   
  7.   
  8. import android.app.Activity;   
  9. import android.os.Bundle;   
  10. import android.view.ContextMenu;   
  11. import android.view.ContextMenu.ContextMenuInfo;   
  12. import android.view.MenuItem;   
  13. import android.view.View;   
  14. import android.view.View.OnCreateContextMenuListener;   
  15. import android.widget.AdapterView;   
  16. import android.widget.AdapterView.AdapterContextMenuInfo;   
  17. import android.widget.AdapterView.OnItemClickListener;   
  18. import android.widget.AdapterView.OnItemLongClickListener;   
  19. import android.widget.AdapterView.OnItemSelectedListener;   
  20. import android.widget.BaseAdapter;   
  21. import android.widget.EditText;   
  22. import android.widget.ListAdapter;   
  23. import android.widget.ListView;   
  24. import android.widget.Toast;   
  25.   
  26. public class ListViewDemo extends Activity {   
  27.        
  28.     //定义Person   
  29.     List<Person> persons ;   
  30.     ListAdapter adapter = null;   
  31.     ListView listView01 = null;   
  32.     EditText editTextPersonName = null;   
  33.        
  34.     //定义上下文菜单     
  35.     final private static int CONTEXT_MENU_EDIT = 0;//菜单[编辑]的编号      
  36.     final private static int CONTEXT_MENU_SHARE = 1;//菜单[分享]的编号      
  37.     final private static int CONTEXT_MENU_DELETE = 2;//菜单[删除]的编号      
  38.     final private static int CONTEXT_MENU_CANCEL = 3;//菜单[取消]的编号    
  39.     private MenuEntity[] contextMenus = {     
  40.             new MenuEntity(0, CONTEXT_MENU_EDIT, 0, "编辑"),     
  41.             new MenuEntity(0, CONTEXT_MENU_SHARE, 1, "分享"),     
  42.             new MenuEntity(0, CONTEXT_MENU_DELETE, 2, "删除"),     
  43.             new MenuEntity(0, CONTEXT_MENU_CANCEL, 3, "取消"),     
  44.             };     
  45.   
  46.     @Override  
  47.     public void onCreate(Bundle savedInstanceState) {   
  48.         super.onCreate(savedInstanceState);   
  49.         setContentView(R.layout.list_view01);   
  50.         //获取组件   
  51.         listView01 = (ListView)findViewById(R.id.listView01);   
  52.         editTextPersonName = (EditText)findViewById(R.id.editText01Edit);   
  53.            
  54.         //显示ListView   
  55.         initListAllPersons();   
  56.         showByMyBaseAdapter();   
  57.            
  58.         //初始化ListView的事件   
  59.         //initListView01Event();   
  60.     }   
  61.        
  62.     public void initListAllPersons(){   
  63.         persons = new ArrayList<Person>();   
  64.         persons.add(new Person(100,"李小龙","香港",android.R.drawable.ic_delete));   
  65.         persons.add(new Person(101,"施瓦辛格","美国",android.R.drawable.ic_dialog_alert));   
  66.         persons.add(new Person(102,"戴安娜王妃","英国",android.R.drawable.ic_dialog_dialer));   
  67.         persons.add(new Person(103,"成龙","香港",android.R.drawable.ic_dialog_map));   
  68.         persons.add(new Person(104,"史泰龙","美国",android.R.drawable.ic_lock_idle_alarm));   
  69.         persons.add(new Person(105,"圣女贞德","法国",android.R.drawable.ic_media_next));   
  70.         persons.add(new Person(106,"列宁","苏联",android.R.drawable.ic_media_play));   
  71.         persons.add(new Person(107,"爱迪生","美国",android.R.drawable.ic_menu_add));   
  72.         persons.add(new Person(108,"孔子","中国",android.R.drawable.ic_media_rew));   
  73.         persons.add(new Person(109,"杨七郎","宋朝",android.R.drawable.ic_menu_zoom));   
  74.         persons.add(new Person(110,"秦始皇","秦国",android.R.drawable.ic_menu_agenda));   
  75.         persons.add(new Person(111,"岳飞","宋朝",android.R.drawable.ic_lock_lock));   
  76.     }   
  77.        
  78.     public void showByMyBaseAdapter(){   
  79.         adapter = new MyBaseAdapter(this, persons);   
  80.         listView01.setAdapter(adapter);   
  81.     }   
  82.   
  83.     /**  
  84.      * 按钮button01Edit的onClick事件.  
  85.      * @param view  
  86.      */  
  87.     public void editPersonAndRefreshListView01(View view){   
  88.         //获取TextEdit数据   
  89.         String value = editTextPersonName.getText().toString();   
  90.         //更新ListView的内容,并且动态刷新.   
  91.         persons.get(0).name=value;   
  92.         ((BaseAdapter) adapter).notifyDataSetChanged();   
  93.     }   
  94.        
  95.        
  96.        
  97.   
  98.     /**  
  99.      * 初始化listView01的事件.  
  100.      */  
  101.     public void initListView01Event(){   
  102.            
  103.         //ListView的item点击事件   
  104.         listView01.setOnItemClickListener(new OnItemClickListener(){   
  105.   
  106.             @Override  
  107.             public void onItemClick(AdapterView<?> parent, View view,   
  108.                     int position, long id) {   
  109.                 Toast.makeText(getApplicationContext(),    
  110.                         "[OnItemClickListener]点击了:"+persons.get(position).name,    
  111.                         Toast.LENGTH_SHORT).show();   
  112.             }   
  113.         });   
  114.            
  115.         //ListView的item长按点击事件   
  116.         listView01.setOnItemLongClickListener(new OnItemLongClickListener(){   
  117.   
  118.             @Override  
  119.             public boolean onItemLongClick(AdapterView<?> parent, View view,   
  120.                     int position, long id) {   
  121.                 Toast.makeText(getApplicationContext(),    
  122.                         "[OnItemLongClickListener]点击了:"+persons.get(position).name,    
  123.                         Toast.LENGTH_SHORT).show();   
  124.                 return false;   
  125.             }   
  126.                
  127.         });   
  128.            
  129.         //ListView的键盘选中事件(直接触摸屏幕选中不会激发)   
  130.         listView01.setOnItemSelectedListener(new OnItemSelectedListener(){   
  131.   
  132.             @Override  
  133.             public void onItemSelected(AdapterView<?> parent, View view,   
  134.                     int position, long id) {   
  135.                 Toast.makeText(getApplicationContext(),    
  136.                         "[OnItemSelectedListener:onItemSelected]点击了:"  
  137. +persons.get(position).name,    
  138.                         Toast.LENGTH_SHORT).show();   
  139.             }   
  140.   
  141.             @Override  
  142.             public void onNothingSelected(AdapterView<?> parent) {   
  143.                 Toast.makeText(getApplicationContext(),    
  144.                         "[OnItemSelectedListener:onNothingSelected]点击了",    
  145.                         Toast.LENGTH_SHORT).show();   
  146.             }   
  147.                
  148.         });   
  149.            
  150.         //ListView组件:创建上下文菜单   
  151.         listView01.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){   
  152.             @Override  
  153.             public void onCreateContextMenu(ContextMenu menu, View v,   
  154.                     ContextMenuInfo menuInfo) {   
  155.                 //获取上下文菜单点击的ListView的位置(position)属性   
  156.                 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;   
  157.                 int position = info.position;   
  158.                    
  159.                 //设置上下文菜单标题   
  160.                 menu.setHeaderTitle(persons.get(position).name);   
  161.                    
  162.                 //设置上下文菜单栏目   
  163.                 MenuEntity menuEntity = null;     
  164.                 for(int i=0;i<contextMenus.length;i++){     
  165.                     menuEntity = contextMenus[i];     
  166.                     menu.add(menuEntity.getGroupId(),menuEntity.getItemId()   
  167. ,menuEntity.getOrder(),menuEntity.getTitle());     
  168.                 }     
  169.             }   
  170.         });   
  171.     }   
  172.   
  173.     //监听选择上下文菜单的选择事件   
  174.     @Override  
  175.     public boolean onContextItemSelected(MenuItem item) {   
  176.         //选中的上下文菜单的itemId   
  177.         int itemId = item.getItemId();   
  178.         //获取上下文菜单点击的ListView的位置(position)属性   
  179.         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();   
  180.         int position = info.position;   
  181.         //Toast   
  182.         Toast.makeText(getApplicationContext(),    
  183.                 "[onContextItemSelected]点击了"+persons.get(position).name+"" +   
  184.                         ",菜单="+contextMenus[itemId].getTitle(),    
  185.                 Toast.LENGTH_SHORT).show();   
  186.         return true;   
  187.     }   
  188.        
  189. }  
package com.zhouzijing.android;

import java.util.ArrayList;
import java.util.List;

import com.zhouzijing.android.util.MenuEntity;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewDemo extends Activity {
	
	//定义Person
	List<Person> persons ;
	ListAdapter adapter = null;
	ListView listView01 = null;
	EditText editTextPersonName = null;
	
	//定义上下文菜单  
    final private static int CONTEXT_MENU_EDIT = 0;//菜单[编辑]的编号   
    final private static int CONTEXT_MENU_SHARE = 1;//菜单[分享]的编号   
    final private static int CONTEXT_MENU_DELETE = 2;//菜单[删除]的编号   
    final private static int CONTEXT_MENU_CANCEL = 3;//菜单[取消]的编号 
	private MenuEntity[] contextMenus = {  
            new MenuEntity(0, CONTEXT_MENU_EDIT, 0, "编辑"),  
            new MenuEntity(0, CONTEXT_MENU_SHARE, 1, "分享"),  
            new MenuEntity(0, CONTEXT_MENU_DELETE, 2, "删除"),  
            new MenuEntity(0, CONTEXT_MENU_CANCEL, 3, "取消"),  
            };  

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view01);
        //获取组件
        listView01 = (ListView)findViewById(R.id.listView01);
        editTextPersonName = (EditText)findViewById(R.id.editText01Edit);
        
        //显示ListView
        initListAllPersons();
        showByMyBaseAdapter();
        
        //初始化ListView的事件
        //initListView01Event();
    }
	
	public void initListAllPersons(){
		persons = new ArrayList<Person>();
		persons.add(new Person(100,"李小龙","香港",android.R.drawable.ic_delete));
		persons.add(new Person(101,"施瓦辛格","美国",android.R.drawable.ic_dialog_alert));
		persons.add(new Person(102,"戴安娜王妃","英国",android.R.drawable.ic_dialog_dialer));
		persons.add(new Person(103,"成龙","香港",android.R.drawable.ic_dialog_map));
		persons.add(new Person(104,"史泰龙","美国",android.R.drawable.ic_lock_idle_alarm));
		persons.add(new Person(105,"圣女贞德","法国",android.R.drawable.ic_media_next));
		persons.add(new Person(106,"列宁","苏联",android.R.drawable.ic_media_play));
		persons.add(new Person(107,"爱迪生","美国",android.R.drawable.ic_menu_add));
		persons.add(new Person(108,"孔子","中国",android.R.drawable.ic_media_rew));
		persons.add(new Person(109,"杨七郎","宋朝",android.R.drawable.ic_menu_zoom));
		persons.add(new Person(110,"秦始皇","秦国",android.R.drawable.ic_menu_agenda));
		persons.add(new Person(111,"岳飞","宋朝",android.R.drawable.ic_lock_lock));
	}
	
	public void showByMyBaseAdapter(){
		adapter = new MyBaseAdapter(this, persons);
		listView01.setAdapter(adapter);
	}

	/**
	 * 按钮button01Edit的onClick事件.
	 * @param view
	 */
	public void editPersonAndRefreshListView01(View view){
		//获取TextEdit数据
		String value = editTextPersonName.getText().toString();
		//更新ListView的内容,并且动态刷新.
		persons.get(0).name=value;
		((BaseAdapter) adapter).notifyDataSetChanged();
	}
	
	
	

	/**
	 * 初始化listView01的事件.
	 */
	public void initListView01Event(){
		
		//ListView的item点击事件
		listView01.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemClickListener]点击了:"+persons.get(position).name, 
						Toast.LENGTH_SHORT).show();
			}
		});
		
		//ListView的item长按点击事件
		listView01.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemLongClickListener]点击了:"+persons.get(position).name, 
						Toast.LENGTH_SHORT).show();
				return false;
			}
			
		});
		
		//ListView的键盘选中事件(直接触摸屏幕选中不会激发)
		listView01.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemSelectedListener:onItemSelected]点击了:"
+persons.get(position).name, 
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemSelectedListener:onNothingSelected]点击了", 
						Toast.LENGTH_SHORT).show();
			}
			
		});
		
		//ListView组件:创建上下文菜单
		listView01.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				//获取上下文菜单点击的ListView的位置(position)属性
				AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
				int position = info.position;
				
				//设置上下文菜单标题
				menu.setHeaderTitle(persons.get(position).name);
				
				//设置上下文菜单栏目
				MenuEntity menuEntity = null;  
                for(int i=0;i<contextMenus.length;i++){  
                    menuEntity = contextMenus[i];  
                    menu.add(menuEntity.getGroupId(),menuEntity.getItemId()
,menuEntity.getOrder(),menuEntity.getTitle());  
                }  
			}
		});
	}

	//监听选择上下文菜单的选择事件
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		//选中的上下文菜单的itemId
		int itemId = item.getItemId();
		//获取上下文菜单点击的ListView的位置(position)属性
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		int position = info.position;
		//Toast
		Toast.makeText(getApplicationContext(), 
				"[onContextItemSelected]点击了"+persons.get(position).name+"" +
						",菜单="+contextMenus[itemId].getTitle(), 
				Toast.LENGTH_SHORT).show();
		return true;
	}
	
}
 

Person类代码

  1. package com.zhouzijing.android;   
  2.   
  3. public class Person {   
  4.        
  5.     public int id;   
  6.     public String name;   
  7.     public String address;   
  8.     public int photo;   
  9.        
  10.     public Person(int id, String name, String address,int photo) {   
  11.         super();   
  12.         this.id = id;   
  13.         this.name = name;   
  14.         this.address = address;   
  15.         this.photo = photo;   
  16.     }   
  17. }  
package com.zhouzijing.android;

public class Person {
	
	public int id;
	public String name;
	public String address;
	public int photo;
	
	public Person(int id, String name, String address,int photo) {
		super();
		this.id = id;
		this.name = name;
		this.address = address;
		this.photo = photo;
	}
}

 

自定义的BaseAdapter类代码

  1. package com.zhouzijing.android;   
  2.   
  3. import java.util.List;   
  4.   
  5. import android.content.Context;   
  6. import android.util.Log;   
  7. import android.view.Gravity;   
  8. import android.view.LayoutInflater;   
  9. import android.view.View;   
  10. import android.view.View.OnClickListener;   
  11. import android.view.View.OnLongClickListener;   
  12. import android.view.ViewGroup;   
  13. import android.widget.BaseAdapter;   
  14. import android.widget.ImageView;   
  15. import android.widget.LinearLayout;   
  16. import android.widget.TextView;   
  17. import android.widget.Toast;   
  18.   
  19. public class MyBaseAdapter extends BaseAdapter {   
  20.     private List<Person> persons;   
  21.     Context context;   
  22.        
  23.     public MyBaseAdapter(Context context,List<Person> persons){   
  24.         this.persons = persons;   
  25.         this.context = context;   
  26.     }   
  27.   
  28.     @Override  
  29.     public int getCount() {   
  30.         return (persons==null)?0:persons.size();   
  31.     }   
  32.   
  33.     @Override  
  34.     public Object getItem(int position) {   
  35.         return persons.get(position);   
  36.     }   
  37.   
  38.     @Override  
  39.     public long getItemId(int position) {   
  40.         return position;   
  41.     }   
  42.        
  43.        
  44.     public class ViewHolder{   
  45.         TextView textViewItem01;   
  46.         TextView textViewItem02;   
  47.         TextView textViewItem03;   
  48.         ImageView imageView;   
  49.     }   
  50.   
  51.     @Override  
  52.     public View getView(int position, View convertView, ViewGroup parent) {   
  53.         final Person person = (Person)getItem(position);   
  54.         ViewHolder viewHolder = null;   
  55.         if(convertView==null){   
  56.             Log.d("MyBaseAdapter", "新建convertView,position="+position);   
  57.             convertView = LayoutInflater.from(context).inflate(   
  58.                     R.layout.list_view01_item, null);   
  59.                
  60.             viewHolder = new ViewHolder();   
  61.             viewHolder.textViewItem01 = (TextView)convertView.findViewById(   
  62.                     R.id.listView01Item01);   
  63.             viewHolder.textViewItem02 = (TextView)convertView.findViewById(   
  64.                     R.id.listView01Item02);   
  65.             viewHolder.textViewItem03 = (TextView)convertView.findViewById(   
  66.                     R.id.listView01Item03);   
  67.                
  68.             //动态增加1个ImageView   
  69.             viewHolder.imageView = new ImageView(context);   
  70.             LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(   
  71.                     LinearLayout.LayoutParams.WRAP_CONTENT,   
  72.                     LinearLayout.LayoutParams.WRAP_CONTENT);   
  73.             mParams.gravity = Gravity.CENTER;   
  74.             mParams.width=50;   
  75.             viewHolder.imageView.setLayoutParams(mParams);   
  76.             //这个ImageView放到ListView的第2列之后   
  77.             ((LinearLayout)convertView).addView(viewHolder.imageView,2);   
  78.                
  79.             convertView.setTag(viewHolder);   
  80.         }else{   
  81.             viewHolder = (ViewHolder)convertView.getTag();   
  82.             Log.d("MyBaseAdapter", "旧的convertView,position="+position);   
  83.         }   
  84.            
  85.         viewHolder.textViewItem01.setText(String.valueOf(person.id));   
  86.         viewHolder.textViewItem02.setText(person.name);   
  87.         viewHolder.textViewItem03.setText(person.address);   
  88.         viewHolder.imageView.setImageResource(person.photo);   
  89.            
  90.         //对ListView中第1个TextView配置OnClick事件   
  91.         viewHolder.textViewItem01.setOnClickListener(new OnClickListener(){   
  92.             @Override  
  93.             public void onClick(View v) {   
  94.                 Toast.makeText(context,    
  95.                         "[textViewItem01.setOnClickListener]点击了"+person.name,    
  96.                         Toast.LENGTH_SHORT).show();   
  97.             }   
  98.         });   
  99.            
  100.         //对ListView中的每一行信息配置OnClick事件   
  101.         convertView.setOnClickListener(new OnClickListener(){   
  102.   
  103.             @Override  
  104.             public void onClick(View v) {   
  105.                 Toast.makeText(context,    
  106.                         "[convertView.setOnClickListener]点击了"+person.name,    
  107.                         Toast.LENGTH_SHORT).show();   
  108.             }   
  109.                
  110.         });   
  111.            
  112.         //对ListView中的每一行信息配置OnLongClick事件   
  113.         convertView.setOnLongClickListener(new OnLongClickListener(){   
  114.             @Override  
  115.             public boolean onLongClick(View v) {   
  116.                 Toast.makeText(context,    
  117.                         "[convertView.setOnLongClickListener]点击了"+person.name,    
  118.                         Toast.LENGTH_SHORT).show();   
  119.                 return true;   
  120.             }   
  121.         });   
  122.            
  123.         return convertView;   
  124.     }   
  125.   
  126. }  
package com.zhouzijing.android;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyBaseAdapter extends BaseAdapter {
	private List<Person> persons;
	Context context;
	
	public MyBaseAdapter(Context context,List<Person> persons){
		this.persons = persons;
		this.context = context;
	}

	@Override
	public int getCount() {
		return (persons==null)?0:persons.size();
	}

	@Override
	public Object getItem(int position) {
		return persons.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	
	public class ViewHolder{
		TextView textViewItem01;
		TextView textViewItem02;
		TextView textViewItem03;
		ImageView imageView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Person person = (Person)getItem(position);
		ViewHolder viewHolder = null;
		if(convertView==null){
			Log.d("MyBaseAdapter", "新建convertView,position="+position);
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_view01_item, null);
			
			viewHolder = new ViewHolder();
			viewHolder.textViewItem01 = (TextView)convertView.findViewById(
					R.id.listView01Item01);
			viewHolder.textViewItem02 = (TextView)convertView.findViewById(
					R.id.listView01Item02);
			viewHolder.textViewItem03 = (TextView)convertView.findViewById(
					R.id.listView01Item03);
			
			//动态增加1个ImageView
			viewHolder.imageView = new ImageView(context);
			LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			mParams.gravity = Gravity.CENTER;
			mParams.width=50;
			viewHolder.imageView.setLayoutParams(mParams);
			//这个ImageView放到ListView的第2列之后
			((LinearLayout)convertView).addView(viewHolder.imageView,2);
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
			Log.d("MyBaseAdapter", "旧的convertView,position="+position);
		}
		
		viewHolder.textViewItem01.setText(String.valueOf(person.id));
		viewHolder.textViewItem02.setText(person.name);
		viewHolder.textViewItem03.setText(person.address);
		viewHolder.imageView.setImageResource(person.photo);
		
		//对ListView中第1个TextView配置OnClick事件
		viewHolder.textViewItem01.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Toast.makeText(context, 
						"[textViewItem01.setOnClickListener]点击了"+person.name, 
						Toast.LENGTH_SHORT).show();
			}
		});
		
		//对ListView中的每一行信息配置OnClick事件
		convertView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Toast.makeText(context, 
						"[convertView.setOnClickListener]点击了"+person.name, 
						Toast.LENGTH_SHORT).show();
			}
			
		});
		
		//对ListView中的每一行信息配置OnLongClick事件
		convertView.setOnLongClickListener(new OnLongClickListener(){
			@Override
			public boolean onLongClick(View v) {
				Toast.makeText(context, 
						"[convertView.setOnLongClickListener]点击了"+person.name, 
						Toast.LENGTH_SHORT).show();
				return true;
			}
		});
		
		return convertView;
	}

}
 

MenuEntity类代码:

 

  1. package com.zhouzijing.android.util;   
  2.   
  3.   
  4. /**  
  5.  * 定义菜单实体结构.  
  6.  * @author stephen  
  7.  *  
  8.  */  
  9. public class MenuEntity{   
  10.        
  11.        
  12.     /**  
  13.      * 生成菜单.  
  14.      * @param groupId  
  15.      * @param itemId  
  16.      * @param order  
  17.      * @param title  
  18.      */  
  19.     public MenuEntity(int groupId, int itemId, int order, CharSequence title) {   
  20.         super();   
  21.         this.groupId = groupId;   
  22.         this.itemId = itemId;   
  23.         this.order = order;   
  24.         this.title = title;   
  25.     }   
  26.        
  27.     /**  
  28.      * 组.  
  29.      */  
  30.     private int groupId;   
  31.     /**  
  32.      * 菜单编号.  
  33.      */  
  34.     private int itemId;   
  35.     /**  
  36.      * 显示顺序号.  
  37.      */  
  38.     private int order;   
  39.     /**  
  40.      * 菜单显示名称.  
  41.      */  
  42.     private CharSequence title;   
  43.     /**  
  44.      * @return the groupId  
  45.      */  
  46.     public int getGroupId() {   
  47.         return groupId;   
  48.     }   
  49.     /**  
  50.      * @param groupId the groupId to set  
  51.      */  
  52.     public void setGroupId(int groupId) {   
  53.         this.groupId = groupId;   
  54.     }   
  55.     /**  
  56.      * @return the itemId  
  57.      */  
  58.     public int getItemId() {   
  59.         return itemId;   
  60.     }   
  61.     /**  
  62.      * @param itemId the itemId to set  
  63.      */  
  64.     public void setItemId(int itemId) {   
  65.         this.itemId = itemId;   
  66.     }   
  67.     /**  
  68.      * @return the order  
  69.      */  
  70.     public int getOrder() {   
  71.         return order;   
  72.     }   
  73.     /**  
  74.      * @param order the order to set  
  75.      */  
  76.     public void setOrder(int order) {   
  77.         this.order = order;   
  78.     }   
  79.     /**  
  80.      * @return the title  
  81.      */  
  82.     public CharSequence getTitle() {   
  83.         return title;   
  84.     }   
  85.     /**  
  86.      * @param title the title to set  
  87.      */  
  88.     public void setTitle(CharSequence title) {   
  89.         this.title = title;   
  90.     }   
  91.        
  92.        
  93.        
  94. }  

posted on 2014-10-14 10:22  qingchen1984  阅读(871)  评论(0编辑  收藏  举报

导航