【问题标题】:Sort ListView not working with custom adapter and with many data in a single item排序 ListView 不适用于自定义适配器和单个项目中的许多数据
【发布时间】:2017-10-14 15:55:45
【问题描述】:

我正在努力使用自定义适配器对我的列表视图进行排序。我一直在这里寻找解决方案,但似乎找不到适合我的问题的解决方案。这是我的代码。

adapter = new ResultListAdapter(this, temList, politicianName, lastName, platform, candidatePosition, votes, image);
    listV = (ListView) findViewById(R.id.candidate_listView);


    Collections.sort(votes, StringDescComparator);
    listV.setAdapter(adapter);


    adapter.notifyDataSetChanged();
}

public static Comparator<String> StringDescComparator = new Comparator<String>() {

    public int compare(String vote1, String vote2) {

        String stringName1 = vote1;
        String stringName2 = vote2;

        return stringName2.compareToIgnoreCase(stringName1);
    }
};

这是我列表的正常视图:

Normal view

And this is my result of my code above

正如您在图片中看到的那样,投票是唯一经过排序的数据,而不是其他数据。 有谁知道在我的代码中要更改或添加什么?

非常感谢您!

【问题讨论】:

  • 您的votes 列表是String 类型的?
  • 是的.. 把它做成字符串对我来说更容易。我认为这不是基于我的变量类型。
  • temList 是什么?我认为您应该对其他内容进行排序,而不是您的选票。我的意思是您的 listView 中的整个实体
  • temList 是派对名称.. 嗯,如果我对整个实体进行排序,它会根据我的排序导致列表中每个项目中的数据乱码..
  • “投票是唯一被排序的数据,而不是其他数据。” 这就是你想要的,不是吗?也对其他数据进行排序,或者我没有得到什么?

标签: java android sorting listview adapter


【解决方案1】:

您的比较器似乎对字符串列表进行了排序。我认为您想对可能在 temList 中的候选人进行排序?更改比较器以通过投票比较候选人。但是您的适配器也有问题它应该将候选者作为一个整体而不是作为单独排序的单独属性列表进行调整。如果我猜对了,这就是候选数据混淆的主要原因。

【讨论】:

  • 正是.. 我的列表混淆的主要原因在于我的适配器。我最好将其更改为整个列表。谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多