比较自定义数组中的项目

时间:2014-02-23 02:48:09

标签: android adapter comparator

我按照教程制作了一个自定义数组,但现在我想改变列表的顺序,我不记得教程所以我只是发布我的代码:

public class AllList {
    public int icon;
    public String pid;
    public String name;
    public String date;
    public String time;
    public String timeAway;
    public String dist;
    public String dist_disp;
    public AllList(){
        super();
    }

    public AllList(int icon, String pid, String name, String date, String time, String timeAway, String dist, String dist_disp) {
        super();
        this.icon = icon;
        this.pid = pid;
        this.name = name;
        this.date = date;
        this.time = time;
        this.timeAway = timeAway;
        this.dist = dist;
        this.dist_disp = dist_disp;
    }
}

和AllListAdapter.java:

public class AllListAdapter extends ArrayAdapter<AllList> {

    Context context;
    int layoutResourceId;   
    ArrayList<AllList> data = null;

    public AllListAdapter(Context context, int layoutResourceId, ArrayList<AllList> allList_data) {
        super(context, layoutResourceId, allList_data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = allList_data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        AllListHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new AllListHolder();
            holder.imgICON = (ImageView)row.findViewById(R.id.list_image);
            holder.txtPID = (TextView)row.findViewById(R.id.pid);
            holder.txtNAME = (TextView)row.findViewById(R.id.name);
            holder.txtDATE = (TextView)row.findViewById(R.id.date);
            holder.txtTIME = (TextView)row.findViewById(R.id.time);
            holder.txtTIMEAWAY = (TextView)row.findViewById(R.id.txtTimeAway);
            holder.txtDIST = (TextView)row.findViewById(R.id.txtDISTNOTDISP);
            holder.txtDIST_DISP = (TextView)row.findViewById(R.id.distance);

            row.setTag(holder);
        }
        else
        {
            holder = (AllListHolder)row.getTag();
        }

        AllList allList = data.get(position);
        holder.imgICON.setImageResource(allList.icon);
        holder.txtPID.setText(allList.pid);
        holder.txtNAME.setText(allList.name);
        holder.txtDATE.setText(allList.date);
        holder.txtTIME.setText(allList.time);
        holder.txtTIMEAWAY.setText(allList.timeAway);
        holder.txtDIST.setText(allList.dist);
        holder.txtDIST_DISP.setText(allList.dist_disp);

        return row;
    }

    static class AllListHolder
    {
        ImageView imgICON;
        TextView txtPID;
        TextView txtNAME;
        TextView txtDATE;
        TextView txtTIME;
        TextView txtTIMEAWAY;
        TextView txtDIST;
        TextView txtDIST_DISP;
    }
}

一切都保存在那里,但现在我想根据另一个变量根据dist或timeaway重新组织它。

这是我到目前为止所拥有的。我尝试用正常的arrayadapter从比较器中改变一些东西:

Collections.sort(allList_data,
                        new Comparator<Map<AllList>>() {

                            @Override
                            public int compare(Map<AllList> o1,
                                    Map<AllList> o2) {
                                String value1;
                                String value2;
                                //EVENT STUFF
                                if(add.equals("Event")){
                                    value1 = o1.get("timeAway");
                                    value2 = o2.get("timeAway");
                                    return Double.valueOf(value1).compareTo(
                                            Double.valueOf(value2));
                                }else{
                                value1 = o1.get("dist");
                                value2 = o2.get("dist");
                                return Double.valueOf(value1).compareTo(
                                        Double.valueOf(value2));
                                }

                            }
                        });

AllList_data是保存所有内容的地方。

提前谢谢你,

泰勒

0 个答案:

没有答案