带自定义对象重新排序的数组列表

时间:2017-06-16 06:14:36

标签: android arraylist

我有一个ArrayList<MediaItem>在媒体项目中有一个Boolean字段名为 IsDownloaded

我想要一个循环,可以按 IsDownloaded 将项目从true归类为false。

1 个答案:

答案 0 :(得分:0)

尝试以下

int ff = 0; // First false index
  CustomObject pf = list.get(0); // photo with false

for (int i = 0; i < list.size(); i++)
  {
    CustomObject p = list.get(i);
    if (p. IsDownloaded())
    {
      // Replace it with the current photo with false
      list.set(i, pf);
      // Put the selected photo in place of the first false photo (and move this index)
      list.set(ff++, p);
      // Next photo is the next to swap (might also advance ff up to the next unselected photo)
      pf = list.get(ff);
    }
  }



for (CustomObject p : list) {
    println(p.selected + "," + p.index);
  }