且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android开发之ListView排序

更新时间:2021-12-13 21:21:36

下面是activity:

[java] view plaincopy
  1. public class MainActivity extends Activity {  
  2.   
  3.     private ListView mListView = null;  
  4.     private List<TestDate> mList = null;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         mListView = (ListView) this.findViewById(R.id.main_listView);  
  11.         mList = new ArrayList<TestDate>();  
  12.         initData();  
  13.         Collections.sort(mList, new Comparator<TestDate>() {  
  14.             /** 
  15.              *  
  16.              * @param lhs 
  17.              * @param rhs 
  18.              * @return an integer < 0 if lhs is less than rhs, 0 if they are 
  19.              *         equal, and > 0 if lhs is greater than rhs,比较数据大小时,这里比的是时间 
  20.              */  
  21.             @Override  
  22.             public int compare(TestDate lhs, TestDate rhs) {  
  23.                 Date date1 = DateUtil.stringToDate(lhs.getDate());  
  24.                 Date date2 = DateUtil.stringToDate(rhs.getDate());  
  25.                 // 对日期字段进行升序,如果欲降序可采用after方法  
  26.                 if (date1.before(date2)) {  
  27.                     return 1;  
  28.                 }  
  29.                 return -1;  
  30.             }  
  31.         });  
  32.         mListView.setAdapter(new MyAdapter(this, mList));  
  33.     }  
  34.   
  35.     private void initData() {  
  36.         mList.add(new TestDate("2012-12-12 12:30""zhangsan"));  
  37.         mList.add(new TestDate("2012-12-12 10:20""lisi"));  
  38.         mList.add(new TestDate("2012-12-11 10:21""lisi"));  
  39.         mList.add(new TestDate("2012-12-11 10:20""lisi"));  
  40.         mList.add(new TestDate("2012-12-13 01:03""wangwu"));  
  41.         mList.add(new TestDate("2012-12-10 02:04""zhaoliu"));  
  42.         mList.add(new TestDate("2012-12-15 23:00""tianqi"));  
  43.         mList.add(new TestDate("2012-11-12 22:30""wangwu"));  
  44.         mList.add(new TestDate("2012-12-17 08:24""shimei"));  
  45.         mList.add(new TestDate("2012-11-10 11:10""shisanmei"));  
  46.         mList.add(new TestDate("2012-12-18 16:50""wangan"));  
  47.         mList.add(new TestDate("2012-12-19 18:00""wangjiu"));  
  48.         mList.add(new TestDate("2012-12-20 19:30""wusi"));  
  49.         mList.add(new TestDate("2012-12-20 19:30""wusi"));  
  50.     }  
  51. }  


下面是工具类:

[java] view plaincopy
  1. public class DateUtil {  
  2.   
  3.     public static Date stringToDate(String dateString) {  
  4.         ParsePosition position = new ParsePosition(0);  
  5.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
  6.         Date dateValue = simpleDateFormat.parse(dateString, position);  
  7.         return dateValue;  
  8.     }  
  9.   
  10. }  


下面是ListView用的Adapter:

[java] view plaincopy
  1. public class MyAdapter extends BaseAdapter {  
  2.   
  3.     private Context mContext;  
  4.     private List<TestDate> mList;  
  5.   
  6.     public MyAdapter(Context context, List<TestDate> list) {  
  7.         this.mContext = context;  
  8.         this.mList = list;  
  9.     }  
  10.   
  11.     @Override  
  12.     public int getCount() {  
  13.         return mList != null ? mList.size() : 0;  
  14.     }  
  15.   
  16.     @Override  
  17.     public Object getItem(int position) {  
  18.         return mList.get(position);  
  19.     }  
  20.   
  21.     @Override  
  22.     public long getItemId(int position) {  
  23.         return position;  
  24.     }  
  25.   
  26.     @Override  
  27.     public View getView(int position, View convertView, ViewGroup parent) {  
  28.         ViewHolder holder = null;  
  29.         if (convertView == null) {  
  30.             convertView = (LinearLayout) LayoutInflater.from(mContext).inflate(  
  31.                     R.layout.main_item, null);  
  32.             holder = new ViewHolder();  
  33.             holder.textView1 = (TextView) convertView  
  34.                     .findViewById(R.id.item_textView1);  
  35.             holder.textVeiw2 = (TextView) convertView  
  36.                     .findViewById(R.id.item_textView2);  
  37.             convertView.setTag(holder);  
  38.         } else {  
  39.             holder = (ViewHolder) convertView.getTag();  
  40.         }  
  41.   
  42.         holder.textView1.setText(mList.get(position).getDate());  
  43.         holder.textVeiw2.setText(mList.get(position).getName());  
  44.   
  45.         return convertView;  
  46.     }  
  47.   
  48.     private class ViewHolder {  
  49.         private TextView textView1;  
  50.         private TextView textVeiw2;  
  51.     }  
  52.   
  53. }  


下面是xml文件:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <ListView  
  7.         android:id="@+id/main_listView"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_centerVertical="true"  
  12.         tools:context=".MainActivity" />  
  13.   
  14. </RelativeLayout>  


 

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/item_textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center_vertical"  
  12.         android:layout_margin="10dp" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/item_textView2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_gravity="center_vertical" />  
  19.   
  20. </LinearLayout>  


下面是一个JavaBean的类:

[java] view plaincopy
  1. public class TestDate {  
  2.       
  3.     private String date;  
  4.     private String name;  
  5.   
  6.     public String getDate() {  
  7.         return date;  
  8.     }  
  9.   
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.   
  14.     public TestDate(String date, String name) {  
  15.         this.date = date;  
  16.         this.name = name;  
  17.     }  
  18.   
  19. }