How to sort RecyclerView item in android

前端 未结 7 1873
暖寄归人
暖寄归人 2021-01-01 11:29

I\'m working on a android chatting application. When I called my api it returns me the chat list sorted by a user_id. But what I need to do is serialized by

7条回答
  •  盖世英雄少女心
    2021-01-01 12:01

     Collections.sort(response.body(), new Comparator() {
                            @Override
                            public int compare(All_posts lhs, All_posts rhs) {
                                if(lhs.getId() > rhs.getId()) {
                                    return -1;
                                } else {
                                    return 1;
                                }
                            }
                        });
    
    • "response.body" is the arraylist I got from json, this is what I pass to the recycler view adapter,

    • "All_posts" is the "Model" class, the one which contains only fields;

    • And getId is the value I want comparison to take place on it, it's from my model class,

    I wrote this before I set my adapter to my recycler view. and after I set my adpater to my recyclerView, I wrote recyclerView.getAdapter().notifyDataSetChanged();

提交回复
热议问题