0

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

RhysBailey21
  • 117
  • 3
  • 13

1 Answers1

0

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.

Orcrist666
  • 266
  • 3
  • 8
  • Yea okay, that seems like a good way of going about it. i Understand how to use intents but not sure on array's. would i just insert each button click onto a new item in the array? how would i go about that? – RhysBailey21 Aug 06 '14 at 00:06
  • An ArrayList isn't exactly like an array. They are easier to use for this kind of situation though. You can create an ArrayList, then adding new items is really easy, just do arrayListName.add(item), where arrayListName is an ArrayList you declared, and item is a matching object that you want to add to the list. In your case, the object could be an object you define, or it could just be a string. That string could just be date + newTotal + pointsAdded, for example. Then in your history Activity, just iterate through the ArrayList printing the items if they are strings. – Orcrist666 Aug 07 '14 at 00:13
  • An ArrayList of strings can be declared like this: ArrayList myList = new ArrayList(); – Orcrist666 Aug 07 '14 at 00:15
  • Probably the best way to go about displaying the history information is to use a ListView in your History Activity, and populate the ListView using the ArrayList and an ArrayAdapter. There's an example here: http://stackoverflow.com/questions/5070830/populating-a-listview-using-arraylist – Orcrist666 Aug 07 '14 at 00:23
  • Yea this seems like it is going to work for me. i've set up the main activity list array but im having trouble sending that data to the new match history activity to then implement into the array adapter. I do not have a button in my main activity to take me straight to the match history activity so i cannot just send the array list via an intent. Although, if i have to save the data to shared preferences, it will only do one string at a time and then just write over the previous everytime new data is entered. what do you suggest i do here to send the data across to the other activity? – RhysBailey21 Aug 09 '14 at 14:22
  • How are you navigating to the match results? I thought you were using a button to take you there, since you said in your post "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**..." My advice is to just add a button. Or, if you have the app navigate to the results activity automatically when a match ends, you can apply the same methods there that you would with a button. I don't advise using shared preferences, that is for saving settings, not transferring data. – Orcrist666 Aug 09 '14 at 20:04
  • yea i have an option in the menu inflator to take me to that activity. its not directly in the main activity class. only accessable in the main activity menu. if i can help it i dont want a button in the main activity layout and i want to be able to access the history log through the match not just at the end. – RhysBailey21 Aug 09 '14 at 23:17
  • That should work just the same, just launch the new Activity in onOptionsItemSelected method, using an Intent and adding extra data as described above, should work fine. – Orcrist666 Aug 11 '14 at 00:24