gpt4 book ai didi

java - 如何在自定义 ListView ArrayList 中进行排序以及如何使其在 Spinner 中工作?

转载 作者:行者123 更新时间:2023-11-30 00:02:24 28 4
gpt4 key购买 nike

我有自定义的 ArrayList,我想用 DescendingAscending Order 用 SpinnerButton 或其他任何东西。

我的 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) {

}

最佳答案

您可以实现 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://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

关于java - 如何在自定义 ListView ArrayList 中进行排序以及如何使其在 Spinner 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49687388/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com