使用多个onclicklistener添加到SharedPreference值 [英] Adding to SharedPreference value with multiple onclicklistener

查看:96
本文介绍了使用多个onclicklistener添加到SharedPreference值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中实现了多个点击侦听器.但是,我希望将来自单独图像的每次点击保存为共享首选项中的"ticker".因此,如果在图像1上有2次点击,在图像2上有4次点击,在图像3上有6次点击,则总计共有12次点击"计入共享偏好.问题是,每个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();
} 
}); 

推荐答案

您要跟踪numclick 3次(在每个OnClickListener内部),因此它们可以相互覆盖是很有意义的.

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

对于初学者,您只能创建一次OnClickListener,并将其分配给每个图像.这应该可以解决:

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

我在这里添加了对您评论的回复,因为我觉得比较清楚.

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

sharedPreferences实例不是问题.它们都与相同的已保存数据("ActivityPREF")通信.问题是您有3个OnClickListener实例,并且所有3个实例都持有整数numClicks.因此,它们都从0(或以前保存的数量)开始,仅增加了本地numClicks.因此,如果我轻按两次image1,则该侦听器中的numClicks将位于2.而其他侦听器仍为0.

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.

如果您在增加numClicks之前在onClick方法中添加了以下内容,那将会奏效:

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

因为它将随后从保存的值中重新加载它.每次单击时,仅会调用onClick方法中的代码,而不是实例化OnClickListener时添加的代码.

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.

这篇关于使用多个onclicklistener添加到SharedPreference值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆