Dowemo


Why do you want to save the parameters with the sharedpreferences, and when you just need to save a single data, consider it.

Example scenario, starting a previously selected navigation application from the application MainActivty, navigation applying the list interface listacitity implementation radio to all navigation applications, and selecting an app for a navigation application then starts the previous selection when starting the main application.

In this case, there may be a number of other ways to choose a better way, for example, to save a navigation list each application and selection state in the database, and the next time you start the main application, it's a common way to do so, but I'd like to know which package name or name, other data, and the person you're using, but may be a stupid approach, but maybe someone might consider doing so, especially a beginner or a.

If you save a checkbox state and application name with a database, it's a relationship, which is a relationship that's.


But when we use sharedpreferences to handle the save parameters, we may encounter the checkbox state confusion, and every time nofiychangedata happens, the main cause is in the getview (

Code example: getview method

...


sv.switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



 @Override


 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


 if (isChecked) {


 PreferenceUtils.setDefaultNaviAppPksName(getItem(position));


 }else{



 PreferenceUtils.setDefaultNaviAppPksName(null);


 }//意思是当选中时保存当前包名,不选时置空


 notifyDataSetChanged();


 }


});//(1









紧接着下面就这样处理状态,由于是单选,只用被选中的才显示选中状态,这种情景是每次notifychagendata或者从其他界面跳转回来,或者下次启动该actvitiy时对以前的数据和状态复原






sv.switchBtn.setChecked(PreferenceUtils.getDefaultNaviAppPksName().


 trim().equals(getItem(position))?true:false);//(2





But like above ( 2 this is caused by doing so, it'll be callback to the above ). A OnCheckedChangeListener 's oncheckedchanged method, which causes the parameter to be empty because in the

notifychagendata的时候会多次执行(2)这一步,









面对类似此情况,应该考虑onCheckedChanged内的处理方法避免重复执行,如果是用户手动改变checkbox状态的话,并没有影响,但是notifychagendata的时候,onCheckedChanged方法就会被动的执行回调,那么内部的处理方法也就被执行了,这种情况由开始我说的一对一关系就不正确了,这分明是多对一关系,多个checkbox状态来改变






onCheckedChanged内部的这一行代码PreferenceUtils.setDefaultNaviAppPksName(null);,所以要一对一关系就要过滤,针对性处理
















这种情况遇见了两三次,对于有些人会是个容易忽略的坑,有时得花上一些时间最后才发现是这里的问题,我就是了,怪我很少用,或者知识不够咯。









就算用其他的方法比如数据库来保存状态数据,也会用到(2)步,所以这个问题值得深思,也许你们会有其他的方法来克服。









处理该方法的思维可以是









1.考虑数据变化关系对等









2.避免方法多次执行









上面我只是1













sv.switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



 @Override


 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


 if (isChecked) {


 PreferenceUtils.setDefaultNaviAppPksName(getItem(position));


 }else{


 if(PreferenceUtils.getDefaultNaviAppPksName()!=null&&


 PreferenceUtils.getDefaultNaviAppPksName().equals(getItem(position)))//(3


 PreferenceUtils.setDefaultNaviAppPksName(null);


 }


 notifyDataSetChanged();


 }


});







加个判断来确认是用户手动改变checkbox状态还是被动notify导致回调状态改变函数,这样一来当nofiy的时候只是针对其他的checkbox状态处理



The first blog post, the principle of so bad and simple blog post.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs