ListView按字母顺序无可比较
问题描述:
我试图按字面值的字母顺序将此ListView放入列。我已经阅读过关于这个主题的答案,但还没有找到任何解决方案。我必须使用Comparable/Comparator吗?ListView按字母顺序无可比较
private ArrayList<HashMap<String, Object>> wordList;
private static final String WORD = "word";
private static final String DEFINITION = "definition";
private static final String NOTES = "notes";
private SimpleAdapter adapter;
ListView listView = (ListView) findViewById(R.id.listView);
final ListView listView2 = (ListView) findViewById(R.id.listView2);
wordList = new ArrayList<>();
HashMap<String, Object> hm;
for (int i = word_counter - 1; i >= 0; i--) {
hm = new HashMap<>();
hm.put(WORD, words[i]);
hm.put(DEFINITION, definitions[i]);
hm.put(NOTES, notes[i]);
wordList.add(hm);
}
adapter = new SimpleAdapter(this, wordList,
R.layout.list_item, new String[]{WORD, DEFINITION, NOTES},
new int[]{R.id.text1, R.id.text2, R.id.text3});
答
对于自动对数据进行排序使用TreeSet中。 TreeSet是一个集合,其中输入的数据将被排序,而不管您输入数据的顺序如何。
TreeSet ts=new TreeSet();
for (int i=0;i<words.length;i++)
{
ts.add(words[i]+"-|-"+definitions[i]+"-|-"+notes[i]);
}
Iterator<String> itr=ts.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
输出将按升序排列。从这里开始,使用“ - | - ”作为split()的参数来分割从TreeSet得到的字符串。根据需要使用每个元素。
现在它的工作,thanx很多! –
不客气。 –