博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android ListView中带有时间数据的排序
阅读量:6975 次
发布时间:2019-06-27

本文共 4274 字,大约阅读时间需要 14 分钟。

下面是activity:

public class MainActivity extends Activity {    private ListView mListView = null;    private List
mList = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) this.findViewById(R.id.main_listView); mList = new ArrayList
(); initData(); Collections.sort(mList, new Comparator
() { /** * * @param lhs * @param rhs * @return an integer < 0 if lhs is less than rhs, 0 if they are * equal, and > 0 if lhs is greater than rhs,比较数据大小时,这里比的是时间 */ @Override public int compare(TestDate lhs, TestDate rhs) { Date date1 = DateUtil.stringToDate(lhs.getDate()); Date date2 = DateUtil.stringToDate(rhs.getDate()); // 对日期字段进行升序,如果欲降序可采用after方法 if (date1.before(date2)) { return 1; } return -1; } }); mListView.setAdapter(new MyAdapter(this, mList)); } private void initData() { mList.add(new TestDate("2012-12-12 12:30", "zhangsan")); mList.add(new TestDate("2012-12-12 10:20", "lisi")); mList.add(new TestDate("2012-12-11 10:21", "lisi")); mList.add(new TestDate("2012-12-11 10:20", "lisi")); mList.add(new TestDate("2012-12-13 01:03", "wangwu")); mList.add(new TestDate("2012-12-10 02:04", "zhaoliu")); mList.add(new TestDate("2012-12-15 23:00", "tianqi")); mList.add(new TestDate("2012-11-12 22:30", "wangwu")); mList.add(new TestDate("2012-12-17 08:24", "shimei")); mList.add(new TestDate("2012-11-10 11:10", "shisanmei")); mList.add(new TestDate("2012-12-18 16:50", "wangan")); mList.add(new TestDate("2012-12-19 18:00", "wangjiu")); mList.add(new TestDate("2012-12-20 19:30", "wusi")); mList.add(new TestDate("2012-12-20 19:30", "wusi")); }}

下面是工具类:

public class DateUtil {    public static Date stringToDate(String dateString) {        ParsePosition position = new ParsePosition(0);        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");        Date dateValue = simpleDateFormat.parse(dateString, position);        return dateValue;    }}

下面是ListView用的Adapter:

public class MyAdapter extends BaseAdapter {    private Context mContext;    private List
mList; public MyAdapter(Context context, List
list) { this.mContext = context; this.mList = list; } @Override public int getCount() { return mList != null ? mList.size() : 0; } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = (LinearLayout) LayoutInflater.from(mContext).inflate( R.layout.main_item, null); holder = new ViewHolder(); holder.textView1 = (TextView) convertView .findViewById(R.id.item_textView1); holder.textVeiw2 = (TextView) convertView .findViewById(R.id.item_textView2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView1.setText(mList.get(position).getDate()); holder.textVeiw2.setText(mList.get(position).getName()); return convertView; } private class ViewHolder { private TextView textView1; private TextView textVeiw2; }}

下面是xml文件:

下面是一个JavaBean的类:

public class TestDate {        private String date;    private String name;    public String getDate() {        return date;    }    public String getName() {        return name;    }    public TestDate(String date, String name) {        this.date = date;        this.name = name;    }}

 

转载地址:http://ojesl.baihongyu.com/

你可能感兴趣的文章
FPGA同步复位,异步复位以及异步复位同步释放实例分析
查看>>
窗体传值
查看>>
《转》从程序员到项目经理(五):不是人人都懂的学习要点
查看>>
如何让做好领导助理工作
查看>>
跟我一起云计算(3)——hbase
查看>>
vim与外部文件的粘帖复制
查看>>
3、数字签名
查看>>
Entity Framework DBFirst尝试
查看>>
dd测试硬盘性能
查看>>
C# 图像处理:实现鼠标选择矩形截图
查看>>
SWIG在系统中安装
查看>>
MySQL在大型网站的应用架构演变
查看>>
C++ 求阶乘 四种方法
查看>>
pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
查看>>
TCPCopy 应用
查看>>
【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)
查看>>
oracle获取字符串长度函数length()和hengthb()
查看>>
linux下启动oracle
查看>>
log4j.properties文件配置--官方文档
查看>>
【原创】开源Math.NET基础数学类库使用(02)矩阵向量计算
查看>>