Search code examples
androidcounterbuttonclickdata-transferscoring

Counter app button click history log


i have spent the last month or so developing a counter app as a companion app for score keeping to a card game that i play.

I am so close to finishing it but i cannot get my head around how i create a history/log of what buttons have been clicked. The app itself has 4 counters (2 per player) and i want an activity that when accessed shows how many times a button has been clicked. For example, if one player has had 5 +1 counters and the other has had 2 +1 counters the history activity should show this, and for each add or subtract thereafter. If one player has +2 and then a few minutes later has another +2 i want it to say the total with the amount added each time next to it with a time that the button was pressed. For example like this:

17:32:12 | 22 | +2

17:34:43 | 25 | +3

17:36:12 | 21 | -4

17:39:51 | 15 | -6, and so on...

The main activity holds all the counters (buttons and textviews) and i have a button in that activity that sends me to the history activity which i want all this data to be displayed in. Here is a snippet or the onClick method for my player1 add button

p1AddL.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_points);
            SharedPreferences muteButton = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            final boolean soundEffects = muteButton.getBoolean("mutebutton", true);
            p1AddL.startAnimation(a);
            if (soundEffects == false)
                buttonSound.start();
            counter1 ++;
            count1 ++;
            if (count1 == 0) {
                if (counter1 >= 9|counter1 <= -1) {
                    lifepointsP1.setText("" + counter1);
                } else
                    lifepointsP1.setText("0" + counter1);
            } else {
                if (count1 > 0) {
                    lifepointsP1.setText("+" + count1);
                    lifepointsP1.setTextColor(Color.GREEN);
                }
                Runnable clickButton = new Runnable() {
                @Override
                public void run() {
                    count1 = 0;
                    lifepointsP1.setTextColor(Color.WHITE);
                    if (counter1 >= 9|counter1 <= -1) {
                        lifepointsP1.setText("" + counter1);
                    } else
                        lifepointsP1.setText("0" + counter1);
                }
            };
            p1AddL.postDelayed(clickButton, 2000);
            }
        }
    });

What this does is when the button is pressed it changes the textview to a green +1, +2, +3, etc. depending on how many times the add button is pressed. After 2 seconds of no button presses it changes the textview back to the total which is displayed in white.

I am sure this is probably a simple data sending and recieving between activities method for both activities but i just can't work it out.

Thanks in advanced for your help


Solution

  • It sounds like you need to use an Intent. An Intent can be used to launch a new Activity, but you can also add data to an Intent to transfer to the new activity.

    Here's an example. You could insert this code in your History button onClick method.

    Intent intent = new Intent(getApplicationContext(), YourHistoryActivity.class);
    intent.putStringArrayListExtra("DATA_KEY", data);
    startActivity(intent);
    

    Then, in the onCreate of the History Activity, you could retrieve the data like this:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        ArrayList<String> data = extras.getStringArrayListExtra("DATA_KEY");
    }
    

    In this example, I used an ArrayList of strings called "data", as that is one way you could store the session scoring information, but there are many options in that regard, and it would depend on how you want to manage your data.

    More info on Intents can be found here and here.