Adding to SharedPreference value with multiple onc

2019-08-31 08:40发布

I have multiple on click listeners implemented in the code. But, I want each click from seperate images to be saved in a "ticker" in shared preferences. So, if there are 2 clicks on image 1, 4 clicks on image 2, and 6 clicks on image 3, it totals up to be 12 "clicks" counted in shared prefs. The problem is, every onClickListener seems to overwrite the other, instead of stacking. Any ideas on how to accomplish this?

Image1.setOnClickListener(new View.OnClickListener() { 
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
int numClicks = pref.getInt("Total_Clicks", 0);

@Override
public void onClick (View v) { 
            numClicks++;
        }

        SharedPreferences pref = 
                            getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        Editor ed = pref.edit();
        ed.putInt("Total_Clicks", numClicks);
        ed.apply();
} 
}); 

Image2.setOnClickListener(new View.OnClickListener() { 
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
int numClicks = pref.getInt("Total_Clicks", 0);

@Override
public void onClick (View w) { 
            numClicks++;
        }

        SharedPreferences pref = 
                            getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        Editor ed = pref.edit();
        ed.putInt("Total_Clicks", numClicks);
        ed.apply();
} 
}); 

Image3.setOnClickListener(new View.OnClickListener() { 
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
int numClicks = pref.getInt("Total_Clicks", 0);

@Override
public void onClick (View x) { 
            numClicks++;
        }

        SharedPreferences pref = 
                            getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        Editor ed = pref.edit();
        ed.putInt("Total_Clicks", numClicks);
        ed.apply();
} 
}); 

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-31 09:10

You are keeping track of the numclicks 3 times (inside each OnClickListener), so it makes sense for them to override each other.

For starters you could create your OnClickListener only once, and assign it to each image. This should solve it:

View.OnClickListener imageClickedListener = new View.OnClickListener() {
        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        int numClicks = pref.getInt("Total_Clicks", 0);

        @Override
        public void onClick (View v) {
            numClicks++;

            Editor ed = pref.edit();
            ed.putInt("Total_Clicks", numClicks);
            ed.apply();
        }


}

Image1.setOnClickListener(imageClickedListener);
Image2.setOnClickListener(imageClickedListener);
Image3.setOnClickListener(imageClickedListener);

EDIT:

I've added a reply to your comment here cause I find it clearer.

The sharedPreferences instances were not the problem. They all talk to the same saved data ("ActivityPREF"). The problem was that you had 3 instances of OnClickListener, and all 3 of them were holding the integer numClicks. So they all started at 0 (or previously saved amount), and only increased the local numClicks. So if I tapped image1 twice, the numClicks inside that listener would be on 2. While the other ones would still be at 0.

It would have worked if you would have added the following to the onClick methods, before increasing the numClicks:

numClicks = pref.getInt("Total_Clicks", 0);

Since it would then reload it from the saved value. Only the code inside the onClick method is called each time a click is made, not the code you add when instantiating an OnClickListener.

查看更多
登录 后发表回答