Android里针对ListView中选项进行排序的方法

针对ListView的各选项进行排序,参考链接:

http://stackoverflow.com/questions/9600040/how-can-i-sort-items-in-baseadapter-before-i-setadapter

实验代码片段如下:

    private void sortInfo() {
        Comparator<InfoTmp> itemComparator = new Comparator<InfoTmp>() {
            public int compare(InfoTmp info1, InfoTmp info2){
                return info1.getAge().compareTo(info2.getAge());
            }
        };
        Collections.sort(mProfileList, itemComparator);
    }

    public class UserListAdapter extends BaseAdapter {
        private Context mContext;
        private List<InfoTmp> mUserList;

        public UserListAdapter(Context context, List<InfoTmp> users) {
            mContext = context;
            mUserList = users;
        }

        public int getCount() {
            return mUserList.size();
        }

        public Object getItem(int position) {
            if (position >= mUserList.size()) {
                return null;
            }
            return mUserList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.user_item, null);
            TextView text = (TextView) convertView.findViewById(R.id.text);
            ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
            ImageButton btnEdit = (ImageButton) convertView.findViewById(R.id.edit);
            ImageButton btnDel = (ImageButton) convertView.findViewById(R.id.delete);
            //... ...
            return convertView;
        }
    }
    private void refreshAdapter() {
        // ... ... 
        sortInfo();
        mListVew.setAdapter(new UserListAdapter(mContext, mProfileList));
    }


你可能感兴趣的:(Android里针对ListView中选项进行排序的方法)