Android按距离

时间:2016-08-30 15:55:41

标签: android sorting adapter

在我的应用程序中,我有一个列表,其中包含自定义项目列表,在适配器的背景中显示不同的项目加载 我正试图用这个代码以增量方式(使用距离值)对适配器的元素进行排序。进入onCretaeView:

cardViewList = new ArrayList<>();
    View vw=inflater.inflate(R.layout.jsonparse_fragment, container, false);
    lv=(ListView) vw.findViewById(R.id.listView1);
    lv.setDivider(null);
    lv.setDividerHeight(0);

    final Double lat = gpsTracker.getLatitude();
    Double LatitudineUber = lat;
    Double lon = gpsTracker.getLongitude();
    Double LongitudineUber = lon;





    cardViewList.clear();

    if (flag_above35==1 && flag_below35==0 && flag_hybrid==0 && flag_position==0 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=1;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
    }
    else if (flag_above35==0 && flag_below35==1 && flag_hybrid==0 && flag_position==0 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=2;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
    }
    else if (flag_above35==1 && flag_below35==0 && flag_hybrid==1 && flag_position==0 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=3;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
    }
    else if (flag_above35==0 && flag_below35==1 && flag_hybrid==1 && flag_position==0 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=4;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
    }
    else if (flag_above35==0 && flag_below35==0 && flag_hybrid==1 && flag_position==0 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=5;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
    }
    else if (flag_above35==0 && flag_below35==0 && flag_hybrid==0 && flag_position==0 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=0;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
        if (flag_uber==1) {
            uber_flag=0;
            new JSONAsynTask1(getContext().getApplicationContext()).execute();
        }
        if (flag_jcd==1) {
            jcd_flag=0;
            //new JSONAsynTask2().execute();
            new JSONAsynTask3().execute();
        }
        if (flag_bitaksi==1) {
            bitaksi_flag=0;
            new JSONAsynTask4(getContext().getApplicationContext()).execute();
        }
        if (flag_lyft==1) {
            lyft_flag = 0;
            new JSONAsynTask5().execute();
        }
    }
    else if (flag_above35==0 && flag_below35==0 && flag_hybrid==0 && flag_position==1 && flag_tempo==0 && flag_conducente == 0)
    {
        car2go_flag=6;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
        if (flag_uber==1) {
            uber_flag=1;
            new JSONAsynTask1(getContext().getApplicationContext()).execute();
        }
        if (flag_jcd==1) {
            jcd_flag=1;
            //new JSONAsynTask2().execute();
            new JSONAsynTask3().execute();
        }
        if (flag_bitaksi==1) {
            bitaksi_flag=1;
            new JSONAsynTask4(getContext().getApplicationContext()).execute();
        }
        if (flag_lyft==1) {
            lyft_flag=1;
            new JSONAsynTask5().execute();
        }
    }
    else if (flag_above35==0 && flag_below35==0 && flag_hybrid==0 && flag_position==0 && flag_tempo==1 && flag_conducente == 0)
    {
        car2go_flag=7;
        new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
        if (flag_uber==1) {
            uber_flag=2;
            new JSONAsynTask1(getContext().getApplicationContext()).execute();
        }
        if (flag_bitaksi==1) {
            bitaksi_flag=2;
            new JSONAsynTask4(getContext().getApplicationContext()).execute();
        }
        if (flag_lyft==1) {
            lyft_flag=2;
            new JSONAsynTask5().execute();
        }
    }
    else if (flag_above35==0 && flag_below35==0 && flag_hybrid==0 && flag_position==0 && flag_tempo==0 && flag_conducente == 1)
    {
        //car2go_flag=8;
        //new JSONAsynTask().execute("https://www.car2go.com/api/v2.1/vehicles?loc="+nome_citta+"&oauth_consumer_key=roadzapp&format=json");
        if (flag_uber==1) {
            uber_flag=1;
            new JSONAsynTask1(getContext().getApplicationContext()).execute();
        }
        if (flag_bitaksi==1) {
            bitaksi_flag=1;
            new JSONAsynTask4(getContext().getApplicationContext()).execute();
        }
        if (flag_lyft==1) {
            lyft_flag=1;
            new JSONAsynTask5().execute();
        }
    }

    adapter = new CardViewAdapter(getActivity(), R.layout.new_item_list, cardViewList);
    Collections.sort(cardViewList, new Comparator<CardView>() {
        @Override
        public int compare(CardView lhs, CardView rhs) {
            Double d1 = Double.parseDouble(lhs.getDistance()) / 1000.0;
            Double d2 = Double.parseDouble(rhs.getDistance()) / 1000.0;
            if (d1 == d2) {
                return 0;
            }
            if (d1 > d2) {
                return 1;
            }
            return -1;
        }
    });


    lv.setAdapter(adapter);

但排序不起作用。 我的代码错了? 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您的cardviewlist正在被AsyncTask填充,因此在填充列表之前调用Collections.sort,因此没有任何内容可以排序。稍后,当AsyncTasks填充列表时,它们不会被排序。