Adolf Kohel Adolf Kohel - 6 months ago 19
Android Question

Error on Using Comparator for Sort ArrayList inside doInBackground Method

i am working on an Android Apps that need to show sorted data by double value. The data is fetched from online database server. So far is going well until i want to sort the data, i read some thread and using Comparator method to sort my arraylist. But, it didn't work. I don't know what happen but there's no error log on my log cat and the data doesn't appear on the screen.

Please help me what i need to do sir?
Thanks.

NB. this the code for my doInBackground method where i doing the process of fetching and sorting the data.

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;
}
}

Answer

Use this:

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