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

时间:2021-08-20 05:09:18

http://stephen830.iteye.com/blog/1141394

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

实现以下功能点:

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

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

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

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

效果图如下:

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

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配置文件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>

Activity类代码:

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

Person类代码

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

自定义的BaseAdapter类代码

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

MenuEntity类代码:

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