按下时菜单按钮滞后很长时间

时间:2011-08-01 01:54:25

标签: android menu android-asynctask

我遇到一个名为“刷新”的菜单按钮问题,按下时会长时间滞后。基本上,我正在运行一个异步任务,从网页下载xml并处理它,然后最终更新当前列表视图。有没有办法防止这种滞后或重构我的代码以提高效率?

以下是我发生刷新问题的活动:

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

这是我的AsyncTask代码:

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

感谢您提供任何帮助。如果我发布过多代码,我会道歉;我不确定找到潜在问题需要多少。

1 个答案:

答案 0 :(得分:2)

查看TimeFeed类的构造函数。既然你说,你正在下载和解析东西,我相信你可能正在构造函数中做这些事情,显然需要一些时间来执行该语句。