我有自定义ArrayList
,我希望使用Spinner
或Button
或其他任何内容对其进行排序降序和升序订单。
我在我的ArrayList中有这个:
reports = new ArrayList<Word>();
reports.add(new Word("AC", "Rektorat", "1101", "3 hari", R.drawable.ac, "AC tidak dingin sama sekali"));
reports.add(new Word("Papan Tulis", "Griya Legita","2502", "5 hari", R.drawable.papantulis, "terdapat noda yang tidak bisa hilang"));
reports.add(new Word("Meja", "Griya Legita","2304", "10 hari", R.drawable.meja, "Berdiri tidak seimbang karena kaki patah"));
reports.add(new Word("Kursi", "Griya Legita","2302", "15 hari", R.drawable.kursi, "senderan patah sebanyak 20 buah"));
reports.add(new Word("Atap", "Griya Legita","2807", "21 hari", R.drawable.atap, "bolong terkena lemparan sepatu"));
reports.add(new Word("Lampu", "Griya Legita","Perpustakaan", "30 hari", R.drawable.lampu, "mati 2 buah"));
reports.add(new Word("Sofa", "Griya Legita","Lobby", "43 hari", R.drawable.sofa, "ketumpahan obat nyamuk"));
CardView rootView = (CardView) findViewById(R.id.root_view);
WordAdapter adapter = new WordAdapter(this, reports, R.color.colorPrimary);
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Serializable data = (Word) listView.getItemAtPosition(position);
Intent intent = new Intent (listView.getContext(), LokasiActivity.class);
intent.putExtra("report", data);
listView.getContext().startActivity(intent);
}
});
字符
public class Word implements Serializable {
private String mAset, mTempat, mRuang, mTanggal, mKomentar;
int mGambar;
public Word (String aset, String tempat, String ruang, String tanggal, int gambar, String komentar) {
mAset = aset;
mTempat = tempat;
mRuang = ruang;
mTanggal = tanggal;
mGambar = gambar;
mKomentar = komentar;
}
public String getAset() {
return mAset;
}
public String getTempat() {
return mTempat;
}
public String getRuang() {
return mRuang;
}
public String getTanggal() {
return mTanggal;
}
public String getKomentar() {
return mKomentar;
}
public int getGambar() {
return mGambar;
}
public void setGambar(int mGambar){
this.mGambar = mGambar;
}
public String toString() {
return mAset + " " + mTempat + " " + mRuang + " " + mTanggal + " " + mGambar ;
}
}
我想对它进行排序,但不知道如何,因为它是自定义的。请帮忙
我尝试将它放在微调器上但不起作用
public void addItemOnSpinner(){
spn = (Spinner) findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("Sort By");
list.add("Descending");
list.add("Ascending");
spn.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if ( spn.getSelectedItem().toString().trim().equals("Descending")) {
Collections.sort(reports, new Comparator<Word>() {
@Override
public int compare(Word o1, Word o2) {
return o2.getAset().compareTo(o1.getAset());
}
});
}
else{
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
答案 0 :(得分:1)
您可以在Comparable<T>
中实施 Word.class
界面
而不是覆盖 compareTo()
方法。
interface Comparable<T> { int compareTo(T obj) }
或实施 Comparator<T>
界面
interface Comparator<T> { int compare(T obj1, T obj2); }
而不是覆盖 compare()
方法。
参见javadoc。我希望它可以帮助你。
https://www.liquibase.org/documentation/changes/sql_file.html https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
答案 1 :(得分:0)
试试这个:
的降序:强>
Collections.sort(reports, new Comparator<Word>() {
@Override
public int compare(Word o1, Word o2) {
return o2.getAset().compareTo(o1.getAset());
}
});
<强>升序:强>
Collections.sort(reports, new Comparator<Word>() {
@Override
public int compare(Word o1, Word o2) {
return o1.getAset().compareTo(o2.getAset());
}
});
然后更新列表:
if ( spn.getSelectedItem().toString().trim().equals("Descending")) {
Collections.sort(reports, new Comparator<Word>() {
@Override
public int compare(Word o1, Word o2) {
return o2.getAset().compareTo(o1.getAset());
}
});
}
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
答案 2 :(得分:0)
如果您想按名称排序:
Collections.sort(atlastContacts, new Comparator<AtlasContact>(){
@Override
public int compare(AtlasContact a1, AtlasContact a2) {
return a1.getName().compareToIgnoreCase(a2.getName());
}
});
答案 3 :(得分:0)
使用Comparator
,如下所示:
Collections.sort(reports, new CustomComparator());
这是您的CustomComparator类:
public class CustomComparator implements Comparator<Case> {
@Override
public int compare(Case o1, Case o2) {
return o1.getContent().compareTo(o2.getContent());
}
}