开发者

Count number of times a button was pressed in 15 minute chunks

My application has 24 buttons to count different vehicle types and directions (the app will be used to count traffic). Currently, I'm saving a line to a .csv file each time a button is pressed. The csv file contains a timestamp, direction, etc.

I have to measure how many times every b开发者_高级运维utton was pressed in 15-minute intervals.

How should I store the counters for the buttons? I just need to output how often every button (every button has a different tag for identification) was pressed in 15 minutes.

I was thinking about using a HashMap, which could just take the button's tag as key and the number of occurrences as value, like this:

HashMap<String, Integer> hm = new HashMap<String, Integer>();

Integer value = (Integer) hm.get(buttonTag);
    if (value == null) {
        hm.put(buttonTag, 1);
    } else {
        int nr_occ = value.intValue() + 1;
        hm.put(buttonTag, nr_occ);
    }

However, I don't need the total sums of button presses, but in 15 minute chunks. And as I'm already writing timestamps to the raw csv-file, I'm wondering how I should store these values.

Edit: I'm currently testing the HashMap, and it's working really well for counting, but I see two issues: first of all, grouping it into 15-minute intervals and secondly, the hashmap isn't sorted in any way. I'd need to output the values sorted by vehicle types and directions.


If your data is simple (i.e. you only need a key/value pair) then consider using SharedPreferences to store the button id and the time it was pressed.

This is a good way because it is extremely fast. You already put the info into a .csv file but to extract the data and traverse through it so that you can compare timestamps is too much overhead IMO.

When a button is pressed store your data in the .csv file then also store the key/value (id/timestamp) then you can iterate through that and do your compare and output whatever you need to output.

The other way (and probably even better) is to simply create and write to your .csv file, then dump it and use something else more robust to process that data as you will probably be doing this anyway.

EDIT: I see a lot of answers which are saying to use SQLite, statics, etc...

These are all inferior methods for what you are asking. Here is why... SQLite is WAY too much for just a simple button id and timestamp. However if you think you might need to store more data in the future this may in fact be an option.

A static variable might be destroyed if the system happens to destroy the Activity.


If you want to be cool, just serialise the HashMap every time your activity gets destroyed (onDestroy) and load it back up when your activity gets created (onCreate). This is going to be the quickest and simplest way.

Where are you serializing your data to? To a file in Environment.getExternalStorage().

Oh and you might want to keep track of a timestamp to clear the HashMap every 15 minutes - you could put this in SharedPreferences.

I assume you already know how to do this but just in case: Java Serialization


Might be me but this sounds as an ideal situation for using a SQLite database. So you can easily get this interval thing from that by selecting on the timestamp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜