[英]How to sort Items in ListView
我有一個ListView
。 每個Item
有 3 個TextViews
。 如何按字母順序對特定id
TextView
的Items
進行排序?
我的Adapter
代碼如下:
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] textName;
private final String[] imagePath;
private final String[] textAge;
private final String[] textSex;
public CustomList(Activity context,
String[] imagePath, String[] textName, String[] textSex, String[] textAge) {
super(context, R.layout.listview_row, textName);
this.context = context;
this.textName = textName;
this.imagePath = imagePath;
this.textAge = textAge;
this.textSex = textSex;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listview_row_mix, null, false);
TextView txtTitle = (TextView) rowView.findViewById(R.id.NameText);
TextView txtSex = (TextView) rowView.findViewById(R.id.SexText);
TextView txtAge = (TextView) rowView.findViewById(R.id.AgeText);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
txtTitle.setText(textName[position]);
txtSex.setText(textSex[position]);
txtAge.setText(textAge[position]);
String root = Environment.getExternalStorageDirectory().toString();
File imageFile = new File(root + "/WorkImages/" + imagePath[position]);
Log.i("Debug", imageFile.toString());
if(imageFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
Log.i("Debug", imageFile.toString());
}else{
imageView.setImageResource(R.drawable.profile_icon);}
return rowView;
}
}
我認為排序應該在adapter
代碼中進行。 我查看了類似的問題,但他們使用 1 TextView
Items
,這意味着他們可以簡單地比較和排序String[]
值。 如果我在這里做類似的事情,我只會弄亂列表,因為名稱和信息不會對應。
使用 Comparator 接口對列表進行排序,然后將數據設置為適配器。
考慮將與單行相關的所有數據(即姓名、圖像路徑、性別和年齡)包裝在一個類中,然后對這些對象的集合進行排序。
您可以使用比較器並對列表數據進行排序
Collections.sort(array, new Comparator<ListData>() {
@Override
public int compare(ListData s1, ListData s2) {
int returnValue = 0;
if (!s1.getName().toLowerCase().startsWith(str.toLowerCase())
&& !s2.getName().toLowerCase().startsWith(str.toLowerCase())
|| s1.getName().toLowerCase().startsWith(str.toLowerCase())
&& s2.getName().toLowerCase().startsWith(str.toLowerCase())) {
returnValue = s1.getName().compareToIgnoreCase(s2.getName());
} else {
if (s1.getName().toLowerCase().startsWith(str.toLowerCase())) {
returnValue = -1;
} else {
returnValue = 1;
}
}
return returnValue;
}
});
我建議您將整個數據集保存在單個數據結構中,使用Comparator
實例對它們進行排序並保存該數據集的副本。
首先創建具有姓名、年齡、性別、imagePath 的 POJO。 這樣您就不必單獨對每個集合進行排序。
public class Person {
public String imagePath;
public String name;
public String age;
public String sex;
}
其次,將這些Person
實例存儲在一個集合中。 假設我們正在使用 java Array,就像您在適配器示例中所做的那樣。
public class CustomList extends ArrayAdapter<String> {
private final Person[] people;
// Other stuff...
}
現在,讓我們添加Comparator
實例和數據集的副本。
public class CustomList extends ArrayAdapter<String> {
private final Person[] people;
private Comparator<Person> comparator;
private Person[] peopleSorted;
// Other stuff...
}
因此,最終的Adapter
實現將類似於
public class CustomList extends ArrayAdapter<String> {
private final Person[] people;
private Comparator<Person> comparator;
private Person[] peopleSorted;
public CustomList(Activity context, Person[] people) {
super(context, R.layout.listview_row, textName);
this.context = context;
this.people = people;
this.peopleSorted = Arrays.copyOf(people, people.length);
}
public void sort(Comparator<Person> comparator) {
if (comparator == null) {
peopleSorted = Arrays.copyOf(people, people.length);
}
else {
Arrays.sort(peopleSorted, comparator);
}
// It is up to you to call notifyDataSetChanged implicitly here, or calling it explicitly from somewhere else.
}
public void getCount() {
return peopleSorted.length;
}
// Other stuff...
}
請注意,排序 API 允許您通過利用多態性來支持開閉原則。 事情將如何排序不再是 Adapter 的責任。
您可以使用直接列表作為以下示例
List<Type> data_source,order;
Collections.sort(data_source);
//or
Collections.sort(data_source, new Sorter(order));
我希望對此有所幫助
我以此作為參考。
相反Dog(String n, int a)
,我寫了一些東西來滿足我的需要。 在這種情況下Dog(String a, String b, String c, String d)
。 然后我添加類似於
public string getDogName(){
return name;
}
喜歡
public string getDogSex(){
return sex;
}
然后在活動中我使用這個:
list.add(new Dog(a[pos], b[pos], c[pos], d[pos]));
其中a, b, c, d
是String[]
排序本身是用
Collections.sort(list);
如果要更改確切排序的內容,請更改此字符串。
return (this.name).compareTo(d.name);
你寫了多種可能性,比如return (this.age).compareTo(d.age);
, 用if
語句包圍它們。
if(counter == 1){
return (this.name).compareTo(d.name);
}
其中 counter 是public static int;
. 該值通過button.onClickListener
更改 - 它允許動態排序。
如果這看起來令人困惑,請嘗試僅使用鏈接中提供的代碼,然后再嘗試修改它。
默認情況下,這將按升序對數據進行排序。
Collections.sort(galaxiesList);
然后我們可以將其反轉為降序,這與升序相反:
Collections.reverse(galaxiesList)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.