在ListView中实现排序
此处介绍的情境是:
(1)使用table布局ListView。
(2)ListView的数据源是List<T>。
(3)排序字段2个(帖子的回复次数和浏览次数),都是int类型。
基本思路:
ListView触发数据源排序,使用数据源(即List<T>)的Sort()方法,又一次绑定数据源到ListView。
实现步骤:
(1)可查知,List<T>的Sort()方法带有一个ICompare<T>泛型接口类型的形參。所以,首先构造继承该泛型接口的类型:
/// <summary>
/// 回复升序比較类
/// </summary>
public class PostReplyCountAscCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return x.ReplyCount.CompareTo(y.ReplyCount);
} #endregion
}
/// <summary>
/// 回复降序比較类
/// </summary>
public class PostReplyCountDescCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return y.ReplyCount.CompareTo(x.ReplyCount);
} #endregion
} /// <summary>
/// 浏览升序比較类
/// </summary>
public class PostViewCountAscCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return x.ViewCount.CompareTo(y.ViewCount);
} #endregion
}
/// <summary>
/// 浏览降序比較类
/// </summary>
public class PostViewCountDescCompare : IComparer<PostInfo>
{
#region IComparer<PostInfo> 成员 public int Compare(PostInfo x, PostInfo y)
{
return y.ViewCount.CompareTo(x.ViewCount);
} #endregion
}
注意:上述的PostInfo模型类,读者能够杜撰,但要有ViewCount和ReplyCount属性(int类型)。
(2)因为有4个排序规则,相应上述(1)中的4个类。所以构造一个排序辅助类:SortHelper,代码例如以下:
public class SortHelper
{
/// <summary>
/// 对集合进行排序——泛型方法
/// </summary>
/// <typeparam name="T1">集合中的对象类型</typeparam>
/// <typeparam name="T2">排序类型</typeparam>
/// <param name="collection">要排序的集合</param>
/// <param name="comparer">排序器</param>
public static void Sort<T1,T2>(List<T1> collection,T2 comparer) where T2:IComparer<T1>
{
collection.Sort(comparer);
}
}
(3)设计ListView,构造排序字段
<LayoutTemplate>
<table class="PostList">
<tr class="PostListHeader">
<td colspan="2">
标题
</td>
<td>
公布日期
</td>
<td>
<asp:LinkButton runat="server" ID="lbtnReply" Text="回复" CommandName="Sort" CommandArgument="ReplyCount"
CssClass="sortLink"></asp:LinkButton>
</td>
<td>
<asp:LinkButton runat="server" ID="lbtnView" Text="浏览" CommandName="Sort" CommandArgument="ViewCount"
CssClass="sortLink"></asp:LinkButton>
</td>
<td>
最后发表
</td>
<td>
删除
</td>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
<div class="pager">
<asp:DataPager ID="pagerBottom" runat="server" PageSize="5">
<Fields>
<asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="<<" PreviousPageText="<"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowLastPageButton="false"
ShowNextPageButton="false" ShowPreviousPageButton="true" />
<asp:NumericPagerField ButtonCount="7" CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"
NumericButtonCssClass="command" />
<asp:NextPreviousPagerField ButtonCssClass="command" LastPageText=">>" NextPageText=">"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowLastPageButton="true"
ShowNextPageButton="true" ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
注意:上面LayoutTemplate中的两个LinkButton,用来作为用户排序接口。其CommandName属性为Sort(固定),CommandArgument分别为ReplyCount和ViewCount。
(4)ListView公开了两个与排序相关的事件:Sorting和Sorted。
/// <summary>
/// listview排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lvPosts_Sorting(object sender, ListViewSortEventArgs e)
{
//推断是否指定了排序字段
if (string.IsNullOrEmpty(e.SortExpression))
{
return;
}
//数据源
if (ViewState["posts"] != null)
{
posts = ViewState["posts"] as List<PostInfo>;
}
else
{
posts = new PostInfoBLL().GetAllPosts(begin, end);
ViewState["posts"] = posts;
}
//升序还是降序
if (ViewState["SortDirection"] != null)
{
e.SortDirection=(SortDirection)ViewState["SortDirection"];
} //按哪个字段排序
if (e.SortExpression == "ReplyCount")
{
if (e.SortDirection == SortDirection.Ascending)
{
//泛型方法调用
SortHelper.Sort<PostInfo, PostReplyCountAscCompare>(posts, new PostReplyCountAscCompare());
ViewState["SortDirection"] = SortDirection.Descending;
}
else
{
SortHelper.Sort<PostInfo, PostReplyCountDescCompare>(posts, new PostReplyCountDescCompare());
ViewState["SortDirection"] = SortDirection.Ascending;
}
}
else if (e.SortExpression == "ViewCount")
{
if (e.SortDirection == SortDirection.Ascending)
{
SortHelper.Sort<PostInfo,PostViewCountAscCompare>(posts, new PostViewCountAscCompare());
ViewState["SortDirection"] = SortDirection.Descending;
}
else
{
SortHelper.Sort<PostInfo,PostViewCountDescCompare>(posts, new PostViewCountDescCompare());
ViewState["SortDirection"] = SortDirection.Ascending;
}
}
BindPosts(true);
}
注意:上述方法中的数据源的获取和BindPosts()方法,读者可自行杜撰。
(5)执行界面例如以下图:

在ListView中实现排序的更多相关文章
- Android入门 在ListView中如何进行精确的定位
在android的开发中,经常会遇到需要主动去设定某条ListItem的位置的需求.设置位置的函数有 ListView.setSelection(int position) ListView.se ...
- ListView 字母导航排序
一.概述 ListView字母导航排序,网上已经有很多代码和博客了, 这篇博文也是照搬网上的. 之所以写到这里,不是为了说明什么,只是为了以后自己查阅方便.本来公司要求实现expandablelis ...
- ImageLoader在Listview中的使用
图片加载框架之ImageLoader 1_特点 1)多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等 2)支持随意的配置ImageLoader,例如线程池 ...
- ListView中的setOnScrollListener
ListView是Android中最常用的控件之一,随着时代发展,RecyclerView有取代它的趋势,但是在一些老代码中,ListView依然扮演着重要的作用.项目中遇到一个需求,需要监听List ...
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- ListView中的数据表格写入Excel中
SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = "xls"; sfd.Filter = "Exce ...
- ListView中动态显示和隐藏Header&Footer
ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...
- Android 如何在 ListView 中更新 ProgressBar 进度
=======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...
- Xamarin.Forms listview中的button按钮,实现带着参数返回上一级页面
今天在做列表显示的时候遇到一个问题,就是在ListView中如何才能让一个button的按钮工作并且包含参数呢? 其实有点类似于rep里的控件无法起获取一样.在Xamarin中,当你button绑定事 ...
随机推荐
- vector的成员函数解析
vector是线性容器,它的元素严格的依照线性序列排序,和动态数组非常相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅能够使用迭代器(iterator)訪问元素,还能够使用指针 ...
- C# 多媒体播放器
//停止播放 public void stopFile() { axWindowsMediaPlayer1.Ctlcontrols.stop(); } //暂停文件 public void pause ...
- AdbWinApi编译详解(本人亲历)
1. 从微软官方下载WDDK,比如:GRMWDK_EN_7600_1.ISO(http://download.microsoft.com/download/4/A/2/4A25C7D5-EFBE-41 ...
- 网络授时服务 NTP
NTP --- Network Time Protocol 网络授时服务,他解决的主要问题就是实现两台或者多台机器的时间同步问题,而传统的格林尼治时间不是标准的时间,因为地球自转的不是规则的. 网络 ...
- 【转】android加载大量图片内存溢出的三种解决办法
方法一: 在从网络或本地加载图片的时候,只加载缩略图. /** * 按照路径加载图片 * @param path 图片资源的存放路径 * @param scalSize 缩小的倍数 * @return ...
- KestrelServer
KestrelServer 跨平台是ASP.NET Core一个显著的特性,而KestrelServer是目前微软推出了唯一一个能够真正跨平台的Server.KestrelServer利用一个名为Ke ...
- Android 修改屏幕解锁方式
Android 修改屏幕解锁方式 问题 在手机第一次开机的时候,运行手机激活的APP 在激活APP允许过程中,当用户按电源键的时候,屏幕黑掉,进入锁屏状态 手机默认的锁屏是滑动解锁 用户这个时候再一次 ...
- vim添加删除多行注释
CTRL+V进入可视化模式 移动光标上移或者下移,选中多行的开头 选择完毕后,按大写的的I键,此时下方会提示进入“insert”模式,输入你要插入的注释符 最后按ESC键,你就会发现多行代码已经被注释 ...
- Linux 经典电子书共享下载
Linux 经典电子书共享下载 Linux网络管理员指南--Linux领域两位领导人物的作品—相当于“Linux 文档项目”.rar vim用户手册_603.0.pdf [Linux系统管理技术手册( ...
- TortoiseSVN 文件关联图标不显示的解决方法
对于SVN来说,因为每个图标都代表着不同的含义,预示着不同的状态,是指示灯的作用,如果没有正确的图标很可能造成数据的丢失等 之前看了网上其他人写的帖子,,有一些是直接删除注册表下“ShellIconO ...