Method call inside a Runnable is not working in android
I need to call the getItem() method inside the runnable. But the thing is it's not working. Both logs are working but the method is not executing.
this.viewItems = new Runnable() {
public void run() {
Log.i("getitem", "before");
getItems();
Log.i("getitem", "done");
}
};
This is getItems method
private void getItems() {
try {
Log.i("url", url);
InputStream source = retrieveStream(this.url);
Log.i("inside getitem", "before object");
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
SearchResponse response = gson.fromJson(reader, SearchResponse.class);
Log.i("inside getitem", "after response");
i_orders = new ArrayList<Items>();
List<Offers> off = response.offers;
for (Offers of : off) {
Items item = new Items();
item.setCovers(of.covers);
item.setCuisine(of.cuisine);
item.setDate_available(of.date_available);
item.setDistance(of.distance);
item.setEnd_time_available(of.end_time_available);
item.setLarge_image_url(of.large_image_url);
item.setLat(of.lat);
item.setLon(of.lon);
item.setMax_cover(of.max_cover);
item.setMin_cover(of.min_cover);
item.setOffer_description(of.offer_description);
开发者_如何转开发 item.setOffer_id(of.offer_id);
item.setPrice_range(of.price_range);
item.setRestaurant_name(of.restaurant_name);
item.setRestaurant_profile(of.restaurant_profile);
item.setRestaurant_suburb(of.restaurant_suburb);
item.setSmall_image_url(of.small_image_url);
item.setStart_time_available(of.start_time_available);
item.setTables_available(of.tables_available);
item.setTime_available(of.time_available);
i_orders.add(item);
}
Thread.sleep(1000);
Log.i("ARRAY", "" + i_orders.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
I don't get even the log message in the top.
Thanks in advance !!
There is an implicit "this." in front of getItems(), but where you're calling from "this" is a different thing (a Runnable, in fact).
You should probably be able to do something like:
private Context myContext;
...
myContext = this;
this.viewItems = new Runnable....
myContext.getItems();
...
But somehow you have to have some kind of variable that is a reference to 'this' in the outer scope that can be used to refer to it from the inner scope.
I figured out that there is a exception inside the getItems method due to not formatted URL.
精彩评论