这个问题是监听器OnCheckChangeListener被调用了两次......这会触发问题,见下文。
public class HomeFragmentActivity extends SherlockFragmentActivity {
...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.id_logout: removeSavedCustomer(this);
Intent i = new Intent(this , SplashActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("activity_change", 0 );
startActivity(i);
break;
case R.id.id_compose:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
ViewGroup view = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.new_message, null);
Spinner select = (Spinner) view.findViewById(R.id.messaging_spinner);
/**
* add spinner adapter and list
*/
ProjectHelper helper = new ProjectHelper(this);
final List<MessageProject> m_project = getSpinnerData(this , helper.getListAsMessage());
// add a dummy
m_project.add( 0 , new MessageProject());
final SpinnerAdapter adapter = new SpinnerAdapter(this , m_project);
select.setAdapter(adapter);
final EditText m_title = (EditText) view.findViewById(R.id.messaging_title);
final EditText m_content = (EditText) view.findViewById(R.id.messaging_content);
final TextView err_spinner = (TextView) view.findViewById(R.id.err_message_spinner);
final TextView err_title = (TextView) view.findViewById(R.id.err_message_title);
final TextView err_content = (TextView) view.findViewById(R.id.err_message_content);
err_spinner.setTextColor(Color.RED);
err_title.setTextColor(Color.RED);
err_content.setTextColor(Color.RED);
m_title.addTextChangedListener(new AfterTextChanged(){
@Override
public void abstract_afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(!isValidString(s.toString())){
error = true;
err_title.setText("Title should not be empty");
}else{
error = false;
err_title.setText("");
}
}
});
m_content.addTextChangedListener(new AfterTextChanged(){
@Override
public void abstract_afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(!isValidString(s.toString())){
error = true;
err_content.setText("Content should not be empty");
}else{
error = false;
err_content.setText("");
}
}
});
Button message = (Button) view.findViewById(R.id.m_btn_message);
Button cancel = (Button) view.findViewById(R.id.m_btn_cancel);
builder.setView(view);
final AlertDialog alert = builder.create();
message.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/**
* messaging action
*/
}
});
cancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
alert.show();
break;
}
return super.onOptionsItemSelected(item);
}
}
public class SpinnerAdapter extends AbstractListAdapter<MessageProject> implements IImageDownload {
protected static final String TAG = "SpinnerAdapter";
private LayoutInflater inflater;
public SpinnerAdapter(Context context, List<MessageProject> lists) {
super(context, lists);
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
@Override
public View getOverridedView(int position, View child, ViewGroup root) {
// TODO Auto-generated method stub
ViewHolder holder = null;
final MessageProject mprojects = getList().get(position);
if(child == null){
holder = new ViewHolder();
child = inflater.inflate(R.layout.message_spinner_projects, null , false);
holder.image = (ImageView) child.findViewById(R.id.mp_image);
holder.name = (TextView) child.findViewById(R.id.mp_project_name);
holder.checkBox = (CheckBox) child.findViewById(R.id.mp_cbox);
child.setTag(holder);
}else{
holder = (ViewHolder) child.getTag();
}
if(position == 0){
holder.name.setText("Select atleast 1");
holder.checkBox.setVisibility(View.INVISIBLE);
holder.image.setVisibility(View.INVISIBLE);
holder.checkBox.setOnCheckedChangeListener(null);
}else{
holder.checkBox.setVisibility(View.VISIBLE);
holder.image.setVisibility(View.VISIBLE);
holder.name.setText(mprojects.getName());
holder.checkBox.setChecked(mprojects.isCheck());
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Log.d(TAG, arg0.toString());
Log.v(TAG, "FROM " + mprojects.isCheck() + " TO " + arg1 );
mprojects.setCheck(arg1);
Log.d(TAG, "Project " + mprojects.getId());
saveSpinnerData(inflater.getContext() , mprojects);
}
});
/*
* download the weak reference bitmap image for imageview
*/
// remove this when online
String replace = mprojects.getImagePath().replace("http://localhost/", Utilities.HOST_NAME);
//
mprojects.setImagePath(replace);
download(mprojects.getImagePath() , holder.image);
}
return child;
}
@Override
public void download(String url, ImageView view) {
// TODO Auto-generated method stub
BitmapDownloaderTask task = new BitmapDownloaderTask(view);
task.execute(url);
}
private class ViewHolder {
ImageView image;
TextView name;
CheckBox checkBox;
}
}
/**
* get the spinner selected projects if there is
*/
public static List<MessageProject> getSpinnerData(Context context , List<MessageProject> data){
SharedPreferences shared = getPreferences(context);
for(MessageProject mproject: data){
Log.d(TAG, "PROEJCT " + mproject.getId() + " = " + shared.getBoolean("message_is_checked_" + mproject.getId(), false));
mproject.setCheck(shared.getBoolean("message_is_checked_" + mproject.getId(), false));
}
return data;
}
/**
* save the spinner selected projects
*/
public static void saveSpinnerData(Context context, MessageProject data){
SharedPreferences shared = getPreferences(context);
SharedPreferences.Editor editor = shared.edit();
if(shared.getLong("project_id_" + data.getId(), -1) != -1){
editor.putBoolean("message_is_checked_"+data.getId() ,data.isCheck() );
}else{
editor.putLong("project_id_" + data.getId(), data.getId());
}
editor.commit();
}
“这里是微调器的图像和复选框,当我按下时我的问题,当我再次按下微调器时,它被调用两次现在数据将是假的,即使它被检查,因为setOnCheckChangeListener onCheckChanged得到不只是一次而是两次。这是怎么回事?......可能的解决办法是什么?“
我现在已经解决了。太糟糕了。
答案 0 :(得分:0)
解决! 已移除onCheckChangedListener 更改为 onClickListener 因为setCheck触发OnCheckChangedListener,愚蠢的我
holder.checkBox.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d(TAG, arg0.toString());
Log.v(TAG, "FROM " + mprojects.isCheck() + " TO " + !mprojects.isCheck() );
mprojects.setCheck(!mprojects.isCheck());
Log.d(TAG, "Project " + mprojects.getId());
saveSpinnerData(inflater.getContext() , mprojects);
}
});
仍然非常感谢任何答案,因为我想严重使用oncheckchangelistener
它只是很奇怪,导致我按下微调器而不是复选框,仍然是oncheckchanged侦听器触发