自定义ListView Adapter多ArrayList按剩余时间排序问题
Great question—this is a super common issue when you split list item data across multiple separate collections. The problem with your current setup is that each ArrayList is independent, so sorting one breaks the index alignment with the others. Let's fix this properly with a clean, maintainable approach:
The best way to keep all your list item data in sync during sorting is to bundle every piece of data for a single list item into a custom model class. This way, each item's name, message, time, imageId, and extras are tied together as one object—sorting the list of these objects will automatically keep all properties aligned.
1. 创建数据模型类
First, define a class to hold all the data for one list item. Let's call it ListItem:
public class ListItem { private String name; private String message; private long time; // 存储毫秒级剩余时间 private String imageId; private String extras; // 构造函数,初始化所有字段 public ListItem(String name, String message, long time, String imageId, String extras) { this.name = name; this.message = message; this.time = time; this.imageId = imageId; this.extras = extras; } // Getter方法,Adapter需要通过这些方法获取数据 public String getName() { return name; } public String getMessage() { return message; } public long getTime() { return time; } public String getImageId() { return imageId; } public String getExtras() { return extras; } }
2. 替换多个ArrayList为单个List
Instead of maintaining five separate ArrayLists, use a single ArrayList<ListItem> to store all your list items:
// 替换原来的5个ArrayList private ArrayList<ListItem> itemList = new ArrayList<>(); // 添加数据时,创建ListItem对象并加入列表 itemList.add(new ListItem("张三", "今天有空吗?", 1800000, "ic_user_zhangsan", "备注:同事")); itemList.add(new ListItem("李四", "项目进度更新", 3600000, "ic_user_lisi", "备注:客户")); // ... 按这种方式添加所有列表项
3. 对List按剩余时间排序
Now you can sort the entire list of ListItem objects using Collections.sort() with a custom comparator that compares the time field. This keeps all related data in sync because each object moves as a whole:
Java 7及更早版本写法:
Collections.sort(itemList, new Comparator<ListItem>() { @Override public int compare(ListItem item1, ListItem item2) { // 按剩余时间从少到多排序 return Long.compare(item1.getTime(), item2.getTime()); } });
Java 8+ 简洁Lambda写法:
Collections.sort(itemList, (item1, item2) -> Long.compare(item1.getTime(), item2.getTime()));
4. 更新Adapter以使用ListItem
In your Adapter's getView() method, retrieve the ListItem at the current position and populate your views using the getter methods:
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { // 加载列表项布局并初始化ViewHolder convertView = LayoutInflater.from(context).inflate(R.layout.your_list_item_layout, parent, false); holder = new ViewHolder(); holder.nameTv = convertView.findViewById(R.id.tv_name); holder.messageTv = convertView.findViewById(R.id.tv_message); holder.timeTv = convertView.findViewById(R.id.tv_time); holder.itemIv = convertView.findViewById(R.id.iv_item); holder.extrasTv = convertView.findViewById(R.id.tv_extras); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // 获取当前ListItem ListItem currentItem = itemList.get(position); // 填充视图数据 holder.nameTv.setText(currentItem.getName()); holder.messageTv.setText(currentItem.getMessage()); // 格式化剩余时间为可读格式(示例) holder.timeTv.setText(formatRemainingTime(currentItem.getTime())); // 根据imageId加载图片(请根据你的图片加载逻辑调整) holder.itemIv.setImageResource(Integer.parseInt(currentItem.getImageId())); holder.extrasTv.setText(currentItem.getExtras()); return convertView; } // 辅助方法:将毫秒转换为可读的剩余时间字符串 private String formatRemainingTime(long milliseconds) { long hours = (milliseconds / 1000) / 3600; long minutes = ((milliseconds / 1000) % 3600) / 60; long seconds = (milliseconds / 1000) % 60; return String.format("%02d:%02d:%02d", hours, minutes, seconds); } // ViewHolder内部类 private static class ViewHolder { TextView nameTv; TextView messageTv; TextView timeTv; ImageView itemIv; TextView extrasTv; }
为什么不推荐单独排序多个ArrayList?
If you tried to sort just the time list and manually reorder the others, you'd have to create a list of indices, sort those indices based on time values, then rebuild each ArrayList using the sorted indices. This is error-prone, hard to maintain, and easy to introduce bugs if you miss updating one of the lists. Using a model class is the standard, clean approach for this scenario.
内容的提问来源于stack exchange,提问作者Parvez Wani

