Android ListView中帶有時間資料的排序

來源:互聯網
上載者:User

標籤:

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

 

Android ListView中帶有時間資料的排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.