Android widget, how to show the next value from the database on a touch event?

in my application I have a database, that I access through a custom ContentProvider, with the following columns:

  • id_widget = the appWidgetId of the widget I would like to show the values on
  • id_item = the id of a value in another table, which I'll get with a SQL JOIN query
  • name_item = the name of the item, assigned by me through the widget configuration activity that I've created.
  • An example of the table could be something like this:

    id_widget | id_item | name­_item
    ----------+---------+----------
        54    |    1    | avaible
        54    |    2    |  used
        58    |    1    |  left
        58    |    3    | avaible2
        58    |    5    |   old
    

    My widgets should show just the first value stored, but when pressed, I'd like them to show the next value and so on until the last one, then they should move to first again. So for example, the widget which has appWidgetId = 58, should show "left" (and another value that I'll get from another table using an SQL JOIN QUERY. Those values are parsed from a site through a IntentService, and saved in another table). When touched, it should show the value "avaible2", then after another touch "old" and if pressed once again it should move to the first value, "left".

    The first idea that I've got was to query the database in order to get all the values, and store them in some Collections inside the AppWidgetProvider, in order to use them in the onUpdate method. That way I wouldn't have to query the database everytime that i touched the widget. That was until i read the doc on AppWidgetProvider and found out how it actually works and the fact that I don't always have the same instance.

    I've been searching for two days for another solution, but I still haven't found anything. The only idea that I have got is the following: - keep track of which row of the table I'm showing (maybe by adding another column to the table) - query the database for the next row everytime that I touch a widget.

    Is it efficient enough? Are there any other ways to solve my problem?

    I apologize for my English, but as this is not my first language, this is the best I could do. Thanks in advance


    First off, your English is great! Wouldn't even have known it wasn't your native language if you hadn't mentioned it. :)

    As you already pointed out in your question, AppWidgetProviders are stateless: you're not guaranteed (in fact, it is unlikely) that you will get the same instance of your AppWidgetProvider class. They are a special type of BroadcastReceiver, which only exists as long as it needs to in order to process the event it is listening for. See this section, in particular.

    The only way to maintain a widget's state/data is to store the needed information persistently, and in Android, that means writing to a file, database, or the app's SharedPreferences (which ends up being a file as well). So you have a couple options with that, but seeing as you already have a database/ContentProvider set up, it would probably make sense to simply add another column, like you mentioned. Ultimately, it needs to be saved somewhere, so even though it's better in general to minimize the number of file/database reads/writes, it is necessary to do so in this case. You could display a ProgressBar on the widget until the query completes, and then display the updated information, especially if you find that the database queries are taking a long time to execute.

    Just for completeness's sake... some widgets use a background Service to maintain and/or update a widget's state. But on its own, this will obviously not persist the widget's state/data when the device is turned off, or in the event that the service is stopped for whatever reason. I don't think this would work well for your situation.

    All that to say, based on my knowledge of widgets, I agree with the approach you've suggested!

    This isn't directly related to your question about handling data, but I found this sample app by Commonsware very helpful in handling widget clicks when I was getting started.

    链接地址: http://www.djcxy.com/p/50658.html

    上一篇: 在Widget Android上的按钮

    下一篇: Android小部件,如何在触摸事件中显示数据库中的下一个值?