在doInBackground方法

时间:2016-05-30 05:04:00

标签: java android sorting arraylist

我正在研究需要按双值显示排序数据的Android应用程序。数据从在线数据库服务器获取。到目前为止,直到我想要对数据进行排序,我阅读了一些线程并使用Comparator方法对我的arraylist进行排序。但是,它没有用。我不知道发生了什么,但我的日志猫没有错误日志,数据也没有显示在屏幕上。

先生,请帮帮我,我需要做什么? 感谢。

NB。这是我的doInBackground方法的代码,我在这里进行获取和排序数据的过程。

    public class getCarRent extends AsyncTask<Void, Void, Void>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        URL hp = null;
        try {

            hp = new URL(getString(R.string.liveurl)
                    + "getCarRent.php?value=" + catId);
            // hp = new URL(
            // "http://192.168.1.106/restourant/foodtype.php?value="
            // + id);
            //Log.d("URL", "" + hp);
            URLConnection hpCon = hp.openConnection();
            hpCon.connect();
            InputStream input = hpCon.getInputStream();
            Log.d("input", "" + input);

            BufferedReader r = new BufferedReader(new InputStreamReader(
                    input));

            String x = "";
            x = r.readLine();
            String total = "";

            while (x != null) {
                total += x;
                x = r.readLine();
            }
         //   Log.d("URL", "" + total);

            JSONArray j = new JSONArray(total);
         //   Log.d("URL1", "" + j.length());
            Item[] itemList = new Item[j.length()];
            resultList = new ArrayList<Item>();
            for (int i = 0; i < j.length(); i++) {
                Item item = new Item();// buat variabel category
                //JSONObject Obj;
                JSONObject Obj = j.getJSONObject(i); //sama sperti yang lama, cman ini lebih mempersingkat karena getJSONObject cm d tulis sekali aja disini

                item.setId(Obj.getString(JF_ID));
                //item.setTitle(Obj.getString(JF_TITLE));


                item.setAddress(Obj.getString(JF_ADDRESS));

                item.setTelephoneNumber(Obj.optString(JF_TELEPHONE, NO_DATA_FOUND));

                item.setEmailAddress(Obj.optString(JF_EMAIL, NO_DATA_FOUND));
                item.setWebUrl(Obj.optString(JF_WEB, NO_DATA_FOUND));
                item.setFacebookUrl(Obj.optString(JF_FACEBOOK, NO_DATA_FOUND));

                item.setLatitude(Obj.optDouble(JF_LATITUDE, NULL_LOCATION));
                item.setLongitude(Obj.optDouble(JF_LONGITUDE, NULL_LOCATION));
                try {
                    item.setRating(Float.parseFloat(Obj.optString(JF_RATING, NO_DATA_FOUND)));
                } catch (NumberFormatException e) {
                    item.setRating(0.0f);
                }

                try {
                    item.setRatingCount(Integer.parseInt(Obj.optString(JF_RATING_COUNT, NO_DATA_FOUND)));
                } catch (NumberFormatException e) {
                    item.setRatingCount(0);
                }

                try {
                    item.setRatingCount(Integer.parseInt(Obj.optString(JF_RATINGSCORE, NO_DATA_FOUND)));
                } catch (NumberFormatException e) {
                    item.setRatingCount(0);
                }


                item.setTagLine(Obj.optString(JF_TAG_LINE, NO_DATA_FOUND));
                item.setDescription(Obj.optString(JF_DESCRIPTION, NO_DATA_FOUND));
                item.setVerification(Obj.optString(JF_VERIFICATION, NO_DATA_FOUND).equals("1") ? true : false);

                item.setCarId(Obj.optString(JF_CARID, NO_DATA_FOUND));
                item.setTitle(Obj.optString(JF_CARTITLE, NO_DATA_FOUND));
                item.setCarRentalId(Obj.optString(JF_CARRENTALID, NO_DATA_FOUND));
                item.setCarPrice(Obj.optString(JF_CARPRICE, NO_DATA_FOUND));
                item.setCarYear(Obj.optString(JF_CARYEAR, NO_DATA_FOUND));

                JSONArray imgArr = Obj.getJSONArray("thumbImage");
                String[] imageThumb = new String[imgArr.length()];
                // String[] imageLarge = new String[imgArr.length()];

                for (int k = 0; k < imgArr.length(); k++) {
                    imageThumb[k] = imgArr.getString(k);
                    // imageLarge[k] = imgArr.getJSONObject(k).getString(JF_TITLE);
                }


                for(int l = 0; l <imgArr.length(); l++) {
                    item.setImageLargeUrls(imageThumb);
                }


                item.setImageThumbUrls(imageThumb);
                // item.setImageLargeUrls(imageLarge);

                //  JSONArray imgArr = Obj.getJSONArray("thumbImage");

                /*String[] imgCount = new String[imgArr.length()];
                for(int k = 0 ; k < imgCount.length; k++) {


                    imgCount[k] = imgArr.getString(k);
                    item.setImageThumbUrls(imgCount);
                }*/


                    Location trgtLocation = new Location("trgtLocation");
                    trgtLocation.setLatitude(item.getLatitude());
                    trgtLocation.setLongitude(item.getLongitude());

                    Log.d("LatLong", "Latitude "+String.valueOf(trgtLocation.getLatitude())+"Longitude "+ String.valueOf(trgtLocation.getLongitude()));

                    Location crntLocation = gTrack.getLocation(getActivity());


                    Log.d("crntLocation", String.valueOf(gTrack.getLocation(getActivity())));

                    item.setDistance(crntLocation.distanceTo(trgtLocation) / 1000);
                    Log.d("distance", String.valueOf(item.getDistance()));


                Log.d("URL1", "" + Obj.getString(JF_TITLE));

                resultList.add(item);

            }

           // itemList;//kayaknya bukan Item atau item

            Arrays.sort(itemList, new Comparator<Item>() {
                @Override
                public int compare(Item lhs, Item rhs) {
                    return lhs.getDistance().compareTo(rhs.getDistance());//mungkin valuenya null
                }
            });

            itemList.notify();

            for (int i = 0; i < j.length(); i++) {

                Log.d("itemList", String.valueOf(itemList[i]));
            }



            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    resultListView.setAdapter(new ResultListAdapterRental(getActivity(), mCallbacks, resultList));
                }
            });

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NullPointerException e) {
            // TODO: handle exception
        }
        return null;
    }
    }

1 个答案:

答案 0 :(得分:0)

使用此:

<div class="max-width">

</div>