自定义的BaseAdapter实现LIstView的展示

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

 

实现以下功能点:

 

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

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

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

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

 

 

 

效果图如下:

 


 

xml配置文件1(list_view01.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"
    >
    <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)

 

<?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类代码:

 

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类代码

 

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类代码

 

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类代码:

package com.zhouzijing.android.util;


/**
 * 定义菜单实体结构.
 * @author stephen
 *
 */
public class MenuEntity{
	
	
	/**
	 * 生成菜单.
	 * @param groupId
	 * @param itemId
	 * @param order
	 * @param title
	 */
	public MenuEntity(int groupId, int itemId, int order, CharSequence title) {
		super();
		this.groupId = groupId;
		this.itemId = itemId;
		this.order = order;
		this.title = title;
	}
	
	/**
	 * 组.
	 */
	private int groupId;
	/**
	 * 菜单编号.
	 */
	private int itemId;
	/**
	 * 显示顺序号.
	 */
	private int order;
	/**
	 * 菜单显示名称.
	 */
	private CharSequence title;
	/**
	 * @return the groupId
	 */
	public int getGroupId() {
		return groupId;
	}
	/**
	 * @param groupId the groupId to set
	 */
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}
	/**
	 * @return the itemId
	 */
	public int getItemId() {
		return itemId;
	}
	/**
	 * @param itemId the itemId to set
	 */
	public void setItemId(int itemId) {
		this.itemId = itemId;
	}
	/**
	 * @return the order
	 */
	public int getOrder() {
		return order;
	}
	/**
	 * @param order the order to set
	 */
	public void setOrder(int order) {
		this.order = order;
	}
	/**
	 * @return the title
	 */
	public CharSequence getTitle() {
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(CharSequence title) {
		this.title = title;
	}
	
	
	
}

  

posted @ 2015-09-12 15:06  regalys168  阅读(174)  评论(0编辑  收藏  举报