使用自定义的BaseAdapter实现LIstView的展示
http://stephen830.iteye.com/blog/1141394
使用自定义的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;
- }
- }
使用自定义的BaseAdapter实现LIstView的展示的更多相关文章
- 使用自定义的BaseAdapter实现LIstView的展示(转)
使用自定义的BaseAdapter实现LIstView的展示 原文链接 http://stephen830.iteye.com/blog/1141394 使用自定义的BaseAdapter实现LIst ...
- 08 BaseAdapter 和ListView总结
第八天 ListView 列表视图 一,特点: >垂直滚动列表,是ViewGroup(容器),列表项使用Adapter填充 二,属性 > android:divider="@dr ...
- WinUI 3学习笔记(2)—— 用ListView来展示集合
在WPF的时代,我们多是使用ListBox和ListView来展示,纵向滚动条显示的集合数据.这两个控件的默认样式,以及对触控的支持,已完全落后于时代.他们两个分别长这样,和Win10及Win11的风 ...
- Android项目实战(十五):自定义不可滑动的ListView和GridView
不可滑动的ListView (RecyclweView类似) public class NoScrollListView extends ListView { public NoScrollListV ...
- 自定义类在PropertyGrid上的展示方法
自定义类在PropertyGrid上的展示方法 零.引言 PropertyGrid用来显示某一对象的属性,但是并不是所有的属性都能编辑,基本数据类型(int, double等)和.Net一些封装的类型 ...
- Android开发之自定义万能BaseAdapter
话不多说哦,直接上模板: package com.zyzpp.adapter; import android.content.Context; import android.util.SparseAr ...
- ListView的自定义适配器及其优化(listView序号混乱问题的处理)
ListView是最常使用的android组件之一,关于listView的优化问题刚刚了解了一些,在这里做出总结. PS:如果想让ListView中的item根据数据内容显示item的大小,需要在it ...
- Android ListView使用BaseAdapter与ListView的优化 (转至 http://www.open-open.com/lib/view/open1339485728006.html)
在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作.也就是说,这个ListView不再只是展示数据,也不仅仅是这一行要来处理用户的操作,而是里面的控件要获得用户的焦点.读者可 ...
- 一个ListView怎么展示两种样式
private class MyBaseMsgAdapter extends BaseAdapter { //获取数据适配器中条目类型的总数,修改成两种(纯文本,输入+文字) @Override pu ...
随机推荐
- Java与WCF交互(一)补充:用WSImport生成WSDL的Java客户端代码
在<Java与WCF交互(一):Java客户端调用WCF服务>一 文中,我描述了用axis2的一个Eclipse控件生成WCF的Java客户端代理类,后来有朋友建议用Xfire.CXF,一 ...
- java 中的Exception RuntimeException 区别
在java的异常类体系中: 1.Error和RuntimeException是非检查型异常,其他的都是检查型异常; 2.所有方法都可以在不声明throws的情况下抛出RuntimeException及 ...
- Filezilla中文字符文件看不到或显示乱码的解决办法
Filezilla确实是跨平台的好软件,可之前我就在ubuntu下郁闷为什么看坛子FTP里竟然是空的.最近换MAC版的FZ结果还是这样就奇怪了. 后来想Filezilla应该是支持字符集转换的,所以在 ...
- <转>数据库设计的三大范式
为了建立冗余较小.结构合理的数据库,设计数据库时必须遵循一定的规则.在关系型数据库中这种规则就称为范式.范式是符合某一种设计要求的总结.要想设计一个结构合理的关系型数据库,必须满足一定的范式. 在实际 ...
- 修改Android手机的“虚拟机堆大小”和android:largeHeap来防止APP内存溢出问题
使用“RAM Manager”修改“虚拟机堆大小”为某一个阀值 xxMB大小 修改 AndroidManifest.xml 里的 Application 标签的属性 android:largeHeap ...
- rman的conver方法拷贝ASM文件
rman中的conver命令主要用户跨平台传输表空间,也可以完成从ASM何本地文件系统中拷贝文件,比用dbms_file_transfer方法要简单 从ASM拷贝到文件系统: 拷贝表空间 在拷贝表空间 ...
- 将spfile存储在ASM中
数据库的spfile开始是存储在普通的文件系统中,如下所示: SQL> show parameter spfile NAME TYPE VALUE ----------------------- ...
- sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...
- windows下安装和配置Weka
Weka是一款免费的,非商业化的,基于java环境下的开源的机器学习以及数据挖掘软件.Weka里含有各种数据挖掘工具:数据预处理,分类与回归,聚类,关联规则和可视化工具. 一.安装weka 我们首先需 ...
- Ensemble learning(集成学习)
集成学习:是目前机器学习的一大热门方向,所谓集成学习简单理解就是指采用多个分类器对数据集进行预测,从而提高整体分类器的泛化能力. 我们在前面介绍了.所谓的机器学习就是通过某种学习方法在假设空间中找到一 ...