此处介绍的情境是:

(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中实现排序的更多相关文章

  1. Android入门 在ListView中如何进行精确的定位

      在android的开发中,经常会遇到需要主动去设定某条ListItem的位置的需求.设置位置的函数有 ListView.setSelection(int position) ListView.se ...

  2. ListView 字母导航排序

    一.概述 ListView字母导航排序,网上已经有很多代码和博客了, 这篇博文也是照搬网上的.  之所以写到这里,不是为了说明什么,只是为了以后自己查阅方便.本来公司要求实现expandablelis ...

  3. ImageLoader在Listview中的使用

    图片加载框架之ImageLoader 1_特点 1)多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等 2)支持随意的配置ImageLoader,例如线程池 ...

  4. ListView中的setOnScrollListener

    ListView是Android中最常用的控件之一,随着时代发展,RecyclerView有取代它的趋势,但是在一些老代码中,ListView依然扮演着重要的作用.项目中遇到一个需求,需要监听List ...

  5. Hadoop学习笔记—11.MapReduce中的排序和分组

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

  6. ListView中的数据表格写入Excel中

    SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = "xls"; sfd.Filter = "Exce ...

  7. ListView中动态显示和隐藏Header&Footer

    ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...

  8. Android 如何在 ListView 中更新 ProgressBar 进度

    =======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...

  9. Xamarin.Forms listview中的button按钮,实现带着参数返回上一级页面

    今天在做列表显示的时候遇到一个问题,就是在ListView中如何才能让一个button的按钮工作并且包含参数呢? 其实有点类似于rep里的控件无法起获取一样.在Xamarin中,当你button绑定事 ...

随机推荐

  1. SignalR系列教程:SignalR快速入门

    ---恢复内容开始--- 本篇是SignalR系列教程的第一篇,本篇内容介绍了如何创建SignalR应用,如何利用SignalR搭建简易的聊天室等,本篇内容参考自:http://www.asp.net ...

  2. 读书笔记:php_tizag_tutorial

    昨天在实验室花了一天时间看了英文版的php_tizag_tutorial,因为上学期用php和bootstrap写过一个租房网站,对php还是比较熟悉.现在总结一下php_tizag_tutorial ...

  3. ModelAndView

    我给你改一下public ModelAndView showDept(HttpServletRequest req,HttpServletResponse resp,ModelMap model){ ...

  4. CCIE路由实验(5) -- BGP负载均衡

    enableconf tno ip do loenable pass ciscoline con 0logg syncexec-t 0 0exitline vty 0 4pass ciscologg ...

  5. 基于visual Studio2013解决C语言竞赛题之1003字母打印

        题目 解决代码及点评 ///************************************************************************/ ...

  6. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  7. 给你的Cordova HybridApp加入Splash启动页面

    如今最新的Cordova 3以上的版本号支持启动画面了,是通过cordova插件实现的. 眼下Splash插件支持android,ios,blackberry等多个平台. 加入插件等步骤例如以下: 加 ...

  8. poj1573&amp;&amp;hdu1035 Robot Motion(模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...

  9. 查看ORACLE事务隔离级别方法(转)

    众所周知,事务的隔离级别有序列化(serializable),可重复读(repeatable read),读已提交(read committed),读未提交(read uncommitted).根据隔 ...

  10. 基于visual Studio2013解决C语言竞赛题之1028平均值

          题目 解决代码及点评 /* 已知有9个数,请求出这些数中的最大值.最小值及平均值,以及有多少个数等于平均值? */ #include<stdio.h> ...