在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绑定事 ...
随机推荐
- iOS-响应上下左右滑动手势
-(void)viewDidLoad{ UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer al ...
- JVM --需要知道的一些术语
1. Java堆中各代分布: Young:主要是用来存放新生的对象. Old:主要存放应用程序中生命周期长的内存对象. Permanent:是指内存的永久保存区域,主要存放Class和Meta的信息, ...
- java--继承的一些笔记
public class Person { public void display(){ System.out.println("Play Person..."); } stati ...
- Android GsonUtils工具类
有那么一个开源jar包,叫gson 可以很方便的将java中的对象和字符串相互转化,数据传输和处理的时候,用到的可能性很大 https://github.com/google/gson http:// ...
- SQL之单行函数
单行函数语法: function name(column|expression,[arg1,arg2,...]) 参数说明: function name:函数名称 column:数据库列名 expre ...
- ASP.NET利用byte检测上传图片安全
) { //这里只测试上传第一张图片file[0] HttpPostedFile file0 = Request.Files[]; //转换成byte,读取图片MIME类型 Stream stream ...
- hdu4725 The Shortest Path in Nya Graph
这道题看了下很多人都是把每一层拆成两个点然后建图做的. 我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度. ...
- HDU 1425 sort 题解
选择出数列中前k个最大的数. 这里由于数据特殊.所以能够使用hash表的方法: #include <cstdio> #include <algorithm> #include ...
- HDU 5046 Airport(DLX反复覆盖)
HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...
- QT对话框中show和exec的区别
转自:http://hi.baidu.com/wangjuns8/blog/item/24b382460dd1c1338694737d.html QDialog的显示有两个函数show()和exec( ...