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

147 篇文章 0 订阅
127 篇文章 0 订阅

针对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));
    }


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值