【问题标题】:Sort RecyclerView by Date按日期排序 RecyclerView
【发布时间】:2018-05-28 09:19:30
【问题描述】:

我正在尝试按日期对 RecyclerView 进行排序,但我尝试了太多东西,我不知道现在该尝试什么。问题出在 adapter.notifyDataSetChanged(); 行,因为如果我不放不会显示错误,但也不会更新 recyclerview

这就是我现在所拥有的,并向我显示了那个错误。

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

首先,我将所有轨迹放在 GetAllTrajects() 方法中,然后在 Collections.sort 中解析 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()

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


}

【问题讨论】:

    标签: java android collections android-recyclerview nullpointerexception


    【解决方案1】:

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

    假设您的日期是 28/05/2018,那么您可以使用以下方法以毫秒为单位获取它:

    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 转换为日期。

     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);
        }
    });
    

    【讨论】:

    • 嘿@ParagPawar,谢谢你回答我。我更新了帖子,问题是因为 Recycler 的大小是 1,我试图做另一件事,但显示了不同的错误
    • 您能否具体说明您遇到的异常情况?
    • 如果您能展示此活动的完整代码,将会很有帮助!
    • 成功了:)
    • 只是一个空指针异常,问题是当我做adapter.notifyDataSetChanged();
    【解决方案2】:

    我认为下面提到的行正在制造一个问题。

    String Date =pendingTrajectPOJO.getDate();

    将日期重命名为其他名称,看看是否有效

    【讨论】:

    • 我只是显示你得到的错误。你能显示 GetAllTrajects() 的详细信息吗?可能是该代码中的某些行导致异常导致 showPendingTraject() 无法执行
    猜你喜欢
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2017-07-05
    • 2016-07-26
    • 2016-10-22
    • 2014-09-07
    • 2018-01-25
    • 1970-01-01
    相关资源
    最近更新 更多