下面是activity:
- public class MainActivity extends Activity {
- private ListView mListView = null;
- private List<TestDate> 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<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,比较数据大小时,这里比的是时间
- */
- @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<TestDate> mList;
- public MyAdapter(Context context, List<TestDate> 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文件:
- <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;
- }
- }