I have a problem with a menu button called “Refresh” that lags for a long time when pressed. Basically, I’m running an async task that downloads xml from a web page and processes it and then finally updates the current list view. Is there some way to prevent this lag or refactor my code to make it more efficient?
Here is my activity where the refresh problem happens:
public class RouteView extends ListActivity implements ActivityUpdater {
private static ArrayList<String> mRouteNames;
private final String TAG = getClass().getName();
private TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route_view);
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
mTv = (TextView) findViewById(R.id.refresh_status);
long start = System.nanoTime();
fetchFeed();
Log.i("MagicBusV2", "Time: " + (System.nanoTime() - start));
}
@Override
protected void onResume() {
super.onResume();
fetchFeed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, Constants.REFRESH_MENU_ID, 0, R.string.refresh).setIcon(R.drawable.refresh);
return result;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean result = super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case Constants.REFRESH_MENU_ID:
long start = System.nanoTime();
fetchFeed();
Log.i("MagicBusV2", "Refresh time: " + (System.nanoTime() - start));
break;
}
return result;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i(TAG, "id: " + (int) id);
Log.i(TAG, "num_routes: " + mRouteNames.size());
String routeName = mRouteNames.get((int) id);
Intent intent = new Intent(this, StopView.class);
intent.putExtra(Constants.ROUTE_NAME, routeName);
intent.putExtra(Constants.STOP_VIEW_TYPE, Constants.STOPS_ROUTE);
startActivity(intent);
}
public void refreshUI() {
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mRouteNames));
getListView().setTextFilterEnabled(true); // allows users to type to filter results, need to inform them of this some way
mTv.setText("Refreshed!");
}
public static void setRouteNames(ArrayList<String> route_names) {
mRouteNames = route_names;
}
private void fetchFeed() {
mTv.setText("Refreshing...");
new RefreshFeedTask(this).execute("null");
}
And here is my AsyncTask code:
public class RefreshFeedTask extends AsyncTask<String, Integer, Void> {
private ArrayList<Route> routes;
private TimeFeed feed;
private ActivityUpdater updater;
private Context mCtx;
public RefreshFeedTask(Context ctx) {
mCtx = ctx;
updater = (ActivityUpdater) mCtx;
routes = new ArrayList<Route>();
try {
feed = new TimeFeed();
} catch (MBusDataException e) {
e.printStackTrace();
}
}
protected Void doInBackground(String... route_name) {
routes = feed.getRoutes();
int num_routes = routes.size();
if(route_name[0].equals("null")) {
ArrayList<String> route_names_list = new ArrayList<String>(num_routes);
for (int i = 0; i < num_routes; i++) {
route_names_list.add(routes.get(i).getName());
}
RouteView.setRouteNames(route_names_list);
} else if(route_name[0].equals("all_stops")) {
ArrayList<Stop> stops = new ArrayList<Stop>();
for (Route route : routes) {
for (Stop stop : route.getStops()) {
if(!stops.contains(stop)) {
stops.add(stop);
}
}
}
StopView.setStops(stops);
} else if(route_name.length > 1) {
Route route = feed.getRouteWithName(route_name[0]);
StopDetailsView.setStop(route.getStopWithName(route_name[1]));
} else if(route_name[0].equals("favorites")) {
ArrayList<Stop> fav_stops = new ArrayList<Stop>();
ArrayList<Favorite> allFavs = FavoritesStore.getInstance(mCtx).getAllFavorites();
for (Favorite fav : allFavs) {
Route route = feed.getRouteWithName(fav.getRouteName());
if (route != null) {
Stop stop = route.getStopWithName(fav.getStopName());
if (stop != null) {
fav_stops.add(stop);
}
}
}
StopView.setStops(fav_stops);
} else {
StopView.setStops(feed.getRouteWithName(route_name[0]).getStops());
}
return null;
}
protected void onPostExecute(Void ignore) {
updater.refreshUI();
}
Thanks for any help you can give. I apologize if I posted too much code; I wasn’t sure how much was needed to find potential problems.
Look at the constructor of the TimeFeed class. Since you said, that you are downloading and parsing stuff, and I believe that you are perhaps doing those things in the constructor, it will obviously take some time to execute that statement.