【发布时间】:2018-08-24 00:50:23
【问题描述】:
我有一个列表视图可以正确添加字符串,但是在我对列表单元格进行排序并调用通知之后,适配器也不会排序。这似乎是合理的,因为我没有链接列表单元和适配器。那么该怎么做呢?
ListView listView = (ListView) findViewById(R.id.viewListViewUsuarios);
List<TextView> listCell = new ArrayList<TextView>();
//(get all TextView and put them on listCell)...
private void reorderArray() {
Collections.sort(listCell, new Comparator<TextView>() {
@Override
public int compare(TextView t1, TextView t2) {
return t1.getText().toString().compareToIgnoreCase(t2.getText().toString());
}
});
CustomAdapter myCustomAdapter = new CustomAdapter();
listView.setAdapter(myCustomAdapter);
}
}
这是我的自定义适配器类
class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return listCell.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.usuarios_aguardando_cell, null);
TextView item = (TextView) view.findViewById(R.id.txtUsuariosAguardandoText);
Button btnInv = (Button) view.findViewById(R.id.btnUsuariosAguardandoViewInvisible);
TextView cell = listCell.get(i);
item.setText(cell.getText());
return view;
}
}
PS:我搜索了其他类似的链接,但没有一个有效。
【问题讨论】:
-
列表排序后是否尝试使用 notifyDataSetChanged?