Improve Spinner Performance

2019-09-05 03:00发布

问题:

I have working activity with 12 spinners that are linked to a single database table of over 20,000 records. Each spinner is bound to a different query to make the selections dynamic (based upon the prior selections). The code works but I'm having awful performance due to the number of queries and size of the table in the database. The initial layout takes 20+ seconds to load. That is because the first spinner is set to an initial selection during the layout which causes the 11 other spinners to populate as well. Performance is also affected when using the spinners. If I go to change the first selection, it takes approximately 10 seconds for all the other spinners to update.

Where should I start in looking for better performance? The database table? The queries? Or should I avoid using 12 spinners?

回答1:

With that many options you should possibly consider going for a drill-down list. This will allow you to split up the queries (one per activity) and will also allow the current list to take up all of the screen. This way you can also provide a way for the user to filter the current list which is probably a must if you have a lot of options in it.

Also:

  • Set your layout first, then start loading your data. At least this way something appears while the user is waiting.
  • Do all long running work in an AsyncTask / Thread to avoid blocking the UI thread
  • Make sure you have primary indexes on your database tables and query using these where possible
  • Don't load all your data at once, just perform each query as needed


回答2:

It sounds like you're not stalling the UI thread, so that's good. You can create indexes to speed up the database, but you'll probably get the biggest performance boost by splitting the queries up instead of doing them all at once. So the first Spinner would be the only enabled control when the Activity launches. Making a selection would fire the query for the second Spinner and enable that one, and so on.

There's a lot of useful performance stuff in this (1 hour) video: http://www.google.com/events/io/2010/sessions/writing-zippy-android-apps.html