Adding to SharedPreference value with multiple onclicklistener

限于喜欢 提交于 2019-12-25 04:52:19

问题


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:


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.



来源:https://stackoverflow.com/questions/19530196/adding-to-sharedpreference-value-with-multiple-onclicklistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!