Tan*_*ove 18 android adapter android-recyclerview
我正在开发一个Android聊天应用程序.当我打电话给我的api时,它会返回按user_id排序的聊天列表.但我需要做的是通过message_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");
}
Run Code Online (Sandbox Code Playgroud)
如果您需要查看完整代码,请https://pastebin.com/Zxmq36Gn
San*_*shi 15
在将列表传递给适配器之前尝试这个(在API调用之后和适配器notifydatasetchanged之前):
Collections.sort(data, new Comparator<CustomData>() {
@Override
public int compare(CustomData lhs, CustomData rhs) {
// -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
return lhs.getId() > rhs.getId() ? -1 : (lhs.customInt < rhs.customInt ) ? 1 : 0;
}
});
Run Code Online (Sandbox Code Playgroud)
在 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
})
Run Code Online (Sandbox Code Playgroud)
之后应用:
myItemAdapter.notifyDataSetChanged()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21695 次 |
| 最近记录: |