Sorting with time data in Android ListView

Source: Internet
Author: User

Here is the activity:

 Public classMainactivity extends Activity {PrivateListView Mlistview =NULL; PrivateList<testdate> mlist =NULL; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mlistview= (ListView) This. Findviewbyid (R.id.main_listview); Mlist=NewArraylist<testdate>();        InitData (); Collections.sort (Mlist,NewComparator<testdate>() {            /** * * * @param LHS * @param RHS * @return An integer < 0 if LHS is L             ESS than RHS, 0 if they is * equal, and > 0 if LHS is greater than RHS, compare data size when compared to time */@Override Public intCompare (TestDate lhs, testdate rhs) {Date date1=dateutil.stringtodate (Lhs.getdate ()); Date Date2=dateutil.stringtodate (Rhs.getdate ()); //The Date field is ascending, and the After method is used if you want to descending                if(Date1.before (date2)) {return 1; }                return-1;        }        }); Mlistview.setadapter (NewMyadapter ( This, mlist)); }    Private voidInitData () {Mlist.add (NewTestDate ("2012-12-12 12:30pm","Zhangsan")); Mlist.add (NewTestDate ("2012-12-12 10:20","Lisi")); Mlist.add (NewTestDate ("2012-12-11 10:21","Lisi")); Mlist.add (NewTestDate ("2012-12-11 10:20","Lisi")); Mlist.add (NewTestDate ("2012-12-13 01:03","Wangwu")); Mlist.add (NewTestDate ("2012-12-10 02:04","Zhaoliu")); Mlist.add (NewTestDate ("2012-12-15 23:00","Tianqi")); Mlist.add (NewTestDate ("2012-11-12 22:30","Wangwu")); Mlist.add (NewTestDate ("2012-12-17 08:24","Shimei")); Mlist.add (NewTestDate ("2012-11-10 11:10","Shisanmei")); Mlist.add (NewTestDate ("2012-12-18 16:50","Wangan")); Mlist.add (NewTestDate ("2012-12-19 18:00","Wangjiu")); Mlist.add (NewTestDate ("2012-12-20 19:30","Wusi")); Mlist.add (NewTestDate ("2012-12-20 19:30","Wusi")); }}

Here is the tool class:

 public  class   Dateutil { 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; }}

Here is the adapter used by the ListView:

 Public classMyadapter extends Baseadapter {PrivateContext Mcontext; PrivateList<testdate>mlist;  PublicMyadapter (context context, list<testdate>list) {         This. Mcontext =context;  This. mlist =list; } @Override Public intGetCount () {returnMlist! =NULL? Mlist.size ():0; } @Override PublicObject GetItem (intposition) {        returnMlist.Get(position); } @Override Public LongGetitemid (intposition) {        returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder Holder=NULL; if(Convertview = =NULL) {Convertview= (linearlayout) layoutinflater. from(Mcontext). Inflate (R.layout.main_item,NULL); Holder=NewViewholder (); 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 ()); returnConvertview; }    Private classViewholder {PrivateTextView TextView1; PrivateTextView textVeiw2; }}

The following is an XML file:

<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>

The following is a JavaBean class:

 Public classTestDate {PrivateString date; PrivateString name;  PublicString getDate () {returndate; }     PublicString GetName () {returnname; }     Publictestdate (string date, string name) { This. Date =date;  This. Name =name; }}

Sorting with time data in Android ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.