使用比较器和自定义 ArrayAdapter 对带有对象的 ArrayList 进行排序
Sorting ArrayList with objects using Comparator and Custom ArrayAdapter
如何使用 Comparator 对对象的 ArrayList 进行正确排序,然后将排序后的数组传递给自定义数组适配器?
我试过这样的:
ArrayList 的内容class:
public class CoursesData {
private String url;
private String name;
private String lessonsCount;
private String isFree;
private String price;
private String language;
private String difficulty;
private AuthorData author;
.....getters-setters
}
比较器:
class CoursesPriceComparator implements Comparator <CoursesData> {
public int compare (CoursesData left, CoursesData right){
return left.getPrice().compareTo(right.getPrice());
}
}
在 OnCreate 中设置适配器:
mAdapter = new CoursesItemAdapter(getActivity(),
R.layout.catalog_list_item,
Collections.sort(parserJson.getCourses(), new CoursesPriceComparator()));
..并在此处出现错误:
...因为我使用的是自定义 ArrayAdapter,它类似于:
class CoursesItemAdapter extends ArrayAdapter<CoursesData> {
private Activity myContext;
private ArrayList<CoursesData> datas;
public CoursesItemAdapter(Context context, int textViewResourceId, ArrayList<CoursesData> objects){
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.catalog_list_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.courses_name);
viewHolder.postDifficultyView = (TextView) convertView.findViewById(R.id.difficulty);
viewHolder.postAuthorView = (TextView) convertView.findViewById(R.id.author);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.postNameView.setText(datas.get(position).getName());
viewHolder.postAuthorView.setText(datas.get(position).getAuthor().getName());
viewHolder.postDifficultyView.setText(datas.get(position).getDifficulty());
return convertView;
}
static class ViewHolder{
TextView postNameView;
TextView postAuthorView;
TextView postDifficultyView;
}
我需要如何修改此适配器以使其与 Comparable 一起使用?
感谢您的帮助。
您对 Collections.sort(
return 排序集合的假设是错误的。
mAdapter = new CoursesItemAdapter(getActivity(),
R.layout.catalog_list_item,
Collections.sort(parserJson.getCourses(), new CoursesPriceComparator()));
应该是
ArrayList< CoursesData > data = parserJson.getCourses();
Collections.sort(parserJson.getCourses(), new CoursesPriceComparator())
mAdapter = new CoursesItemAdapter(getActivity(),
R.layout.catalog_list_item,
data);
Collection.sort
return 类型无效。在这里查看 documentation
如何使用 Comparator 对对象的 ArrayList 进行正确排序,然后将排序后的数组传递给自定义数组适配器?
我试过这样的:
ArrayList 的内容class:
public class CoursesData {
private String url;
private String name;
private String lessonsCount;
private String isFree;
private String price;
private String language;
private String difficulty;
private AuthorData author;
.....getters-setters
}
比较器:
class CoursesPriceComparator implements Comparator <CoursesData> {
public int compare (CoursesData left, CoursesData right){
return left.getPrice().compareTo(right.getPrice());
}
}
在 OnCreate 中设置适配器:
mAdapter = new CoursesItemAdapter(getActivity(),
R.layout.catalog_list_item,
Collections.sort(parserJson.getCourses(), new CoursesPriceComparator()));
..并在此处出现错误:
...因为我使用的是自定义 ArrayAdapter,它类似于:
class CoursesItemAdapter extends ArrayAdapter<CoursesData> {
private Activity myContext;
private ArrayList<CoursesData> datas;
public CoursesItemAdapter(Context context, int textViewResourceId, ArrayList<CoursesData> objects){
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.catalog_list_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.courses_name);
viewHolder.postDifficultyView = (TextView) convertView.findViewById(R.id.difficulty);
viewHolder.postAuthorView = (TextView) convertView.findViewById(R.id.author);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.postNameView.setText(datas.get(position).getName());
viewHolder.postAuthorView.setText(datas.get(position).getAuthor().getName());
viewHolder.postDifficultyView.setText(datas.get(position).getDifficulty());
return convertView;
}
static class ViewHolder{
TextView postNameView;
TextView postAuthorView;
TextView postDifficultyView;
}
我需要如何修改此适配器以使其与 Comparable 一起使用?
感谢您的帮助。
您对 Collections.sort(
return 排序集合的假设是错误的。
mAdapter = new CoursesItemAdapter(getActivity(),
R.layout.catalog_list_item,
Collections.sort(parserJson.getCourses(), new CoursesPriceComparator()));
应该是
ArrayList< CoursesData > data = parserJson.getCourses();
Collections.sort(parserJson.getCourses(), new CoursesPriceComparator())
mAdapter = new CoursesItemAdapter(getActivity(),
R.layout.catalog_list_item,
data);
Collection.sort
return 类型无效。在这里查看 documentation