所以我正在尝试对一个简单的arrayadapter listview进行排序。这是我尝试过的:
ListView lv;
String[] months = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
ArrayAdapter<String> adapter;
ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById (R.id.listView1);
lv.setFastScrollEnabled(true);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.sortasc:
list = new ArrayList<String>(Arrays.asList(months));
Collections.sort(list);
for(int i=0; i < list.size(); i++){
System.out.println(list.get(i));
}
adapter.clear();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
adapter.notifyDataSetChanged();
return true;
case R.id.sortdesc:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
但是我得到UnsupportedOperationException,这有什么问题?我真的需要帮助。提前谢谢!
答案 0 :(得分:20)
在对项目进行排序后,您没有调用setAdapter(适配器)。
使用如下
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
而不是
adapter.clear(); // Useless
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
adapter.notifyDataSetChanged();
答案 1 :(得分:13)
这是诀窍......
private void sortAscending () {
List<String> sortedMonthsList = Arrays.asList(months);
Collections.sort(sortedMonthsList);
months = (String[]) sortedMonthsList.toArray();
}
对于sortAscending,请致电
sortAscending();
adapter.notifyDatasetChanged();
同样,您也可以实现自定义Comparator以降序排序......
希望这有助于......:)
答案 2 :(得分:8)
Comparator<String> ALPHABETICAL_ORDER1 = new Comparator<String>() {
public int compare(String object1, String object2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(object1.toString(), object2.toString());
return res;
}
};
Collections.sort(your array name, ALPHABETICAL_ORDER1);
答案 3 :(得分:3)
listAdapter = new AppsAdapter(MainActivity.this,R.layout.content_layout,appList);
final Collator col = Collator.getInstance();
listAdapter.sort(new Comparator<ApplicationInfo>() {
@Override
public int compare(ApplicationInfo lhs, ApplicationInfo rhs) {
return col.compare(lhs.loadLabel(packageManager),rhs.loadLabel(packageManager));
}
});
答案 4 :(得分:1)
答案 5 :(得分:1)
要对列表进行排序,您需要使用Comparator。您需要使用按字母顺序排序的比较器对基础集合进行排序,然后通知列表进行更新。
以下是使用ArrayAdapter执行此操作的示例:https://stackoverflow.com/a/8920348/1369222
答案 6 :(得分:0)
如果您已经有另一个适配器,只需在适配器声明
下复制粘贴此代码即可adapter.sort(new Comparator<String>() {
@Override
public int compare(String arg1, String arg0) {
return arg1.compareTo(arg0);
}
});
答案 7 :(得分:0)
您可以在Collection中添加数据,而不是直接将数据添加到适配器。比使用压缩器对集合进行排序。最后...
adapter.addAll(collectionData);