[英]Best way for to sort listview
I have fragment with ListView connected to custom ArrayAdapter.我有连接到自定义 ArrayAdapter 的 ListView 片段。 Also on this fragment I have TextView for to sort items by name.
同样在这个片段上,我有 TextView 用于按名称对项目进行排序。 Currently it works in next style, when I input any text in this TextView I'm changing sorting order for SQL request, like so: On first positions I'm showing items which contain "entered text" and after that all other items.
目前它在下一个样式中工作,当我在此 TextView 中输入任何文本时,我正在更改 SQL 请求的排序顺序,如下所示:在第一个位置我显示包含“输入文本”的项目,然后显示所有其他项目。
Now I'm doing it from my view, by stupid way, I'm every time reselect data from database, with ordering by some specific order field, which = 1 if Name field contain "entered text" and = 0 if not contain.现在我从我的角度来看,以愚蠢的方式,我每次都从数据库中重新选择数据,并按某些特定的订单字段排序,如果名称字段包含“输入的文本”,则 = 1,如果不包含,则 = 0。
Can somebody tell me if it's possible to sort ArrayList in this style without reselect data from database?有人能告诉我是否可以在不从数据库中重新选择数据的情况下以这种方式对 ArrayList 进行排序吗?
Here is my solution:这是我的解决方案:
if (mActualFilter.equals("")) {
Collections.sort(mProductItems, new Comparator<ProductItem>() {
@Override
public int compare(ProductItem o1, ProductItem o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
} else {
Collections.sort(mProductItems, new Comparator<ProductItem>() {
@Override
public int compare(ProductItem o1, ProductItem o2) {
String mName1 = o1.getName();
String mName2 = o2.getName();
if ((mName1.toLowerCase().contains(mActualFilter.toLowerCase())) && (!mName2.toLowerCase().contains(mActualFilter.toLowerCase()))) {
return -1;
} else if ((!mName1.toLowerCase().contains(mActualFilter.toLowerCase())) && (mName2.toLowerCase().contains(mActualFilter.toLowerCase()))) {
return 1;
} else {
return 0;
}
}
});
}
Does the data change according to user's input?数据是否根据用户的输入而改变?
If it changes, then you have no option but to re-select data.如果它发生变化,那么您别无选择,只能重新选择数据。
If it doesn't change, you can sort the Array using java.util.Collections.sort(array, comparator)
(and you can create custom comparators that sort based on the fields you need)如果它没有改变,您可以使用
java.util.Collections.sort(array, comparator)
对 Array 进行排序(并且您可以创建根据您需要的字段进行排序的自定义比较器)
Example (supposing I have a class with 2 fields):示例(假设我有一个包含 2 个字段的类):
class MyClass {
private String someField;
private int otherField;
// getters and setters, etc..
}
I can create a comparator that compares someField
:我可以创建一个比较
someField
的比较器:
Comparator<MyClass> someFieldComparator = new Comparator<MyClass>() {
@Override
public int compare(MyClass m1, MyClass m2) {
return m1.getSomeField().compareTo(m2.getSomeField());
}
};
Method compare
must return -1
if m1
is "less than" m2
(which means, if m1
must be before m2
when I sort), 1
if m1
is "greater than" m2
( m1
must be after m2
when I sort), or 0
if they are considered "equal" (it doesn't matter the order of m1
and m2
when I sort).方法
compare
必须返回-1
如果m1
是“小于” m2
(这意味着,如果m1
必须早m2
时我排序), 1
如果m1
是“大于” m2
( m1
必须是后m2
时我排序),或0
如果它们被认为是“相等的”(排序时m1
和m2
的顺序无关紧要)。 In this case, I used compareTo
of String
class, because it does this same logic with strings.在这种情况下,我使用了
String
类的compareTo
,因为它对字符串执行相同的逻辑。 If I call Collections.sort(list, someFieldComparator)
, it will sort according to values of someField
.如果我调用
Collections.sort(list, someFieldComparator)
,它将根据someField
值进行someField
。
For otherField
, I could create another comparator:对于
otherField
,我可以创建另一个比较器:
Comparator<MyClass> otherFieldComparator = new Comparator<MyClass>() {
@Override
public int compare(MyClass m1, MyClass m2) {
int v1 = m1.getOtherField();
int v2 = m2.getOtherField();
if (v1 < v2) {
return -1;
}
if (v1 > v2) {
return 1;
}
return 0;
}
};
Note that I used the same logic of returning -1
, 1
or 0
as described above.请注意,我使用了与上述相同的返回
-1
、 1
或0
逻辑。
So you can create as many comparators as needed and use them accordingly.因此,您可以根据需要创建尽可能多的比较器并相应地使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.