下面是activity:

public class MainActivity extends Activity {private ListView mListView = null;private List<TestDate> mList = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mListView = (ListView) this.findViewById(R.id.main_listView);mList = new ArrayList<TestDate>();initData();Collections.sort(mList, new Comparator<TestDate>() {/** *  * @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,比较数据大小时,这里比的是时间 */@Overridepublic 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<TestDate> mList;public MyAdapter(Context context, List<TestDate> list) {this.mContext = context;this.mList = list;}@Overridepublic int getCount() {return mList != null ? mList.size() : 0;}@Overridepublic Object getItem(int position) {return mList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/main_listView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        tools:context=".MainActivity" /></RelativeLayout>


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/item_textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:layout_margin="10dp" />    <TextView        android:id="@+id/item_textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" /></LinearLayout>


下面是一个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;}}


更多相关文章

  1. [置顶] android对字段的验证
  2. ANDROID的MANIFEST.XML文件字段解析
  3. android rom短信模块最后获取的Cursor字段内容
  4. xml字段含义
  5. Oracle Clob字段过长保存出错改如何解决?
  6. 对比select * 和 select 所有字段写出速度实例
  7. Asp.net MVC 对输入的字符串字段做Trim处理的方法_实用技巧
  8. golang json怎么隐藏某个字段
  9. Golang中使用JSON时区分空字段和未设置字段的方法

随机推荐

  1. Android(安卓)Multimedia框架总结(二十七)M
  2. Notification 通知栏的使用
  3. [转]Android(安卓)Studio的logcat窗口被
  4. Android(安卓)weixin service
  5. android onTouchEvent 左右手势滑动事件
  6. How To Backup Your Android(安卓)Phone
  7. How to Install apk to Android(安卓)Dev
  8. Android使用图片资源
  9. android中,如果使用imageButton可以在draw
  10. Windows下修改android 模拟机 hosts文件