Adding to SharedPreference value with multiple onclicklistener

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(); } });

以上就是Adding to SharedPreference value with multiple onclicklistener的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏