如何在android中对RecyclerView项目进行排序

如何在android中对RecyclerView项目进行排序,android,android-recyclerview,adapter,Android,Android Recyclerview,Adapter,我正在开发一个android聊天应用程序。当我调用api时,它会返回按用户id排序的聊天列表。但我需要做的是按消息id进行序列化,因为我想先显示最后一条消息。下面是我的onBindViewHolder方法,我在其中获取值 public void onBindViewHolder(final MyAdapter_HomeViewHolder holder, final int position) { holder.userNameTV.setText(data.get(position)

我正在开发一个android聊天应用程序。当我调用api时,它会返回按用户id排序的聊天列表。但我需要做的是按消息id进行序列化,因为我想先显示最后一条消息。下面是我的onBindViewHolder方法,我在其中获取值

public void onBindViewHolder(final MyAdapter_HomeViewHolder holder, final int position) {

    holder.userNameTV.setText(data.get(position).getUserInfo().getFullName());
    holder.msgBodyTV.setText(data.get(position).getBody());
    holder.originator_iD.setText(data.get(position).getUserInfo().getId().toString());

    //message ID. I need to serialize my recyclerView by this ID.biggest id should appear first.
    holder.messageId.setText(data.get(position).getId().toString());

    holder.owner_type_ET.setText("1");
    holder.subject_ET.setText("Message");

}

如果您需要查看完整代码,请在将数据传递到
RecyclerView
适配器之前

data.sort(new Comparator<Datum>() {
            @Override
            public int compare(Datum o1, Datum o2) {
                return o1.get(position).getMessageId().compareTo(o2.get(position).getMessageId());
            }
        });
data.sort(新的比较器(){
@凌驾
公共整数比较(基准o1、基准o2){
返回o1.get(position.getMessageId().compareTo(o2.get(position.getMessageId());
}
});

然后将已排序的列表传递(通知)到适配器。

在将列表传递到适配器之前(在API调用之后和适配器notifydatasetchanged之前),请尝试以下操作:

Collections.sort(数据,新比较器(){
@凌驾
公共整数比较(CustomData lhs、CustomData rhs){
//-1-小于,1-大于,0-等于,降序时全部反转
返回lhs.getId()>rhs.getId()?-1:(lhs.customInt
在传递到RecyclerView适配器之前添加这行代码

Collections.sort(yourLists, new Comparator<YourList>() {
        @Override
        public int compare(YourList lhs, YourList rhs) {
            return lhs.getId().compareTo(rhs.getId());
        }
    });
Collections.sort(yourLists,newcomparator(){
@凌驾
公共整数比较(YourList lhs,YourList rhs){
返回lhs.getId().compareTo(rhs.getId());
}
});
Collections.sort(response.body(),new Comparator()){
@凌驾
公共整数比较(所有左后、所有右后){
if(lhs.getId()>rhs.getId()){
返回-1;
}否则{
返回1;
}
}
});
  • “response.body”是我从json获得的arraylist,这是我传递的 到回收器视图适配器

  • “All_posts”是“Model”类,只包含字段

  • getId是我想要比较的值,它来自我的模型类

在将适配器设置为回收器视图之前,我写了这篇文章。
在我将Adpeter设置为我的recyclerView之后,我编写了
recyclerView.getAdapter().notifyDataSetChanged()

在Kotlin中,在数组中加载数据后使用如下方式:

myItems.sortWith(Comparator { lhs, rhs ->
            // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
            if (lhs.name > rhs.name) -1 else if (lhs.id < rhs.id) 1 else 0
        })
Listitems\u模型;
Collections.sort(items\u model,newcomparator(){
@凌驾
公共整数比较(项目详细信息模型o1,项目详细信息模型o2){
返回o1.getDate().compareTo(o2.getDate());
}
});

对于仍停留在同一条道路上的人来说,这是一个更简单的解决方案

  Collections.sort(data, new Comparator<CustomData>() {
            @Override
            public int compare(CustomData lhs, CustomData rhs) {
                return Integer.compare( rhs.getId(),lhs.getId());
            }
        });

YourAdapter adapter = new YourAdapter(context, data);

//Setup Linear or Grid Layout manager
 recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 recyclerView.setAdapter(adapter);

Collections.sort(数据,新比较器(){
@凌驾
公共整数比较(CustomData lhs、CustomData rhs){
返回Integer.compare(rhs.getId(),lhs.getId());
}
});
YourAdapter=新的YourAdapter(上下文、数据);
//设置线性或栅格布局管理器
setLayoutManager(新的LinearLayoutManager(getContext());
recyclerView.setAdapter(适配器);

您是指序列化还是排序?我假设您使用的是回收器视图,所以您需要做的(假设您是指排序)是在添加新项时对列表进行排序,然后在适配器上调用“notifyDataSetChanged()”。这可以通过在recycler视图中为对象实现一个comparator函数来实现。对于如何排序,我指的是短消息id。需要在顶部显示最大消息id。notifyDataSetChanged()将一如既往地返回我的用户id。请查看我发布的链接,了解如何对列表进行排序。在构造函数中,您可以在将列表设置为成员变量之前对其进行排序:
public MyAdapter\u home(列表数据,int rowLayout,Context Context){//sort data here this.data=data;this.rowLayout=rowLayout;this.Context=Context;}
我做了但没有成功。希望您看到我上面提到的完整代码。data.sort不起作用。它说,api的使用记录为@since 1.8+,只需使用@Sanat Chandravanshi回答以下问题,而不是使用return lhs.getId()>rhs.getId()-1:(左S.customInt<右S.customInt)?1 : 0; 使用Integer.compare(rhs.attribute(),lhs.attribute())会显示许多错误。你能建议我什么将是我的代码根据你?工程完美。谢谢,什么是customInt?我有一个id并通过getId方法获取它Hi Ali,customInt是您比较的可比getter,而不是使用```return lhs.getId()>rhs.getId()-1:(左S.customInt<右S.customInt)?1 : 0; ``` 使用``Integer.compare(rhs.attribute(),lhs.attribute())```
myItems.sortWith(Comparator { lhs, rhs ->
            // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
            if (lhs.name > rhs.name) -1 else if (lhs.id < rhs.id) 1 else 0
        })
myItemAdapter.notifyDataSetChanged()
 List<Items_Detail_model>items_model;
  Collections.sort(items_model, new Comparator<Items_Detail_model>() {
            @Override
            public int compare(Items_Detail_model o1, Items_Detail_model o2) {
                return o1.getDate().compareTo(o2.getDate());
            }
        });
  Collections.sort(data, new Comparator<CustomData>() {
            @Override
            public int compare(CustomData lhs, CustomData rhs) {
                return Integer.compare( rhs.getId(),lhs.getId());
            }
        });

YourAdapter adapter = new YourAdapter(context, data);

//Setup Linear or Grid Layout manager
 recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 recyclerView.setAdapter(adapter);