按日期对RecyclerView排序 [英] Sort RecyclerView by Date

查看:493
本文介绍了按日期对RecyclerView排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按日期对RecyclerView进行排序,但是我尝试了太多事情,现在不知道该怎么做.问题在 adapter.notifyDataSetChanged(); 行中,因为如果我不输入,不会显示错误但也不会更新recyclerview

I'm trying to sort a RecyclerView by Date, but I tried too many things and I don't know what to try now. The problem is in the line adapter.notifyDataSetChanged(); because if I don't put doesn't show me the error but doesn't update the recyclerview too

这就是我现在所拥有的,并向我显示该错误.

This is what I have now, and shows me that error.

无法启动活动ComponentInfo {st.stc/sharetaxi.sharetaxicabdriver.PendingTrajectRecyclerView}:java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v7.widget.RecyclerView $ Adapter.notifyDataSetChanged()'空对象引用"

Unable to start activity ComponentInfo{st.stc/sharetaxi.sharetaxicabdriver.PendingTrajectRecyclerView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged()' on a null object reference'

首先,我将所有轨迹放入方法 GetAllTrajects()中,然后将其放入Collections.sort内,以分析TextView的日期.

First of all I put all the trajects in the method GetAllTrajects() and inside Collections.sort I parse to date the TextView.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pending_traject_recycler_view);

    listPendingTraject=new ArrayList<>();
    recyclerPendingTraject = (RecyclerView)findViewById(R.id.pendingTrajectRecyclerView);
    recyclerPendingTraject.setLayoutManager(new LinearLayoutManager(this));

      GetAllTrajects();

    Collections.sort(listPendingTraject, new Comparator<PendingTrajectPOJO>() {
        @Override
        public int compare(PendingTrajectPOJO pendingTrajectPOJO, PendingTrajectPOJO t1) {
            String Date = pendingTrajectPOJO.getDate();

            String Date1 = t1.getDate();

            Date date=null,date1=null;

            SimpleDateFormat formatDate = new SimpleDateFormat("dd/MMM/yyyy");
            try {
                date = formatDate.parse(Date);
                date1 = formatDate.parse(Date1);
            } catch (ParseException e) {
                e.printStackTrace();
            }


            return date.before(date1) ? 1 : 0;

        }
    });
    adapter.notifyDataSetChanged();
}

当我放置所有信息时,在内部方法GetAllTrajects中调用方法 showPendingTraject()

Inside method GetAllTrajects when I put all the information I call the method showPendingTraject()

  private void showPendingTraject() {
    listPendingTraject.add(new PendingTrajectPOJO(nameUsers,coordenatesOriginBundle,Origin,Destination,DataIHora,coordenatesDestiBundle,contadorCordenades));

    Log.d("ID",""+nomUser);
    adapter = new AdapterPendingTraject(listPendingTraject);
    recyclerPendingTraject.setAdapter(adapter);


}

推荐答案

一个简单的解决方案是在PendingTrajectPOJO存储日期中存储/输入数据(以毫秒为单位),该日期将是long类型而不是String,然后您可以轻松比较长值并将其排序.

A simple solution to it would be while storing/entering the data in your PendingTrajectPOJO store date in milliseconds which will be of type long instead of String, and then you can easily compare the long values, and sort it.

假设您的日期为2018年5月28日,则可以使用以下方法以毫秒为单位获取日期:

Suppose your date is 28/05/2018, then you can get it in milliseconds using the following method:

String givenDateString = "28/05/2018"; 
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
   Date mDate = sdf.parse(givenDateString);
   long timeInMilliseconds = mDate.getTime();
   //store this timeInMilliseconds in your POJO object
   System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
        e.printStackTrace();
}

并使用timeInMilliseconds对其进行排序,只是不要忘记在RecyclerView中显示时将timeInMilliseconds转换为日期.

and using timeInMilliseconds sort it, just don't forget to convert timeInMilliseconds into a date while displaying in RecyclerView.

 Collections.sort(listPendingTraject, new Comparator<PendingTrajectPOJO>() {
    @Override
    public int compare(PendingTrajectPOJO pendingTrajectPOJO, PendingTrajectPOJO t1) {

        long Date = pendingTrajectPOJO.getDate();
        long Date1 = t1.getDate();

        return Date.compareTo(Date1);
    }
});

这篇关于按日期对RecyclerView排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆