Sayandh Sayandh - 1 year ago 32
Android Question

Switch State Change Does Not Work On The First Click

If I change any switch state the

DatabaseReference
doesn't change from the previous value the first time... if I change the same switch state one more time it works perfectly. Here is my code:

public sigAdapter(Context context, String[] web, Integer[] imageId) {
super(context, R.layout.list, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
DatabaseReference databaseReference1 = database.getReference("data").child(web[position1].replace("/",""));
databaseReference=databaseReference1.child("Switch");
ViewHolder viewHolder=new ViewHolder();

View listItemView = view;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list, parent, false);

viewHolder.txtTitle = (TextView) listItemView.findViewById(R.id.text_view);
viewHolder.imageView = (ImageView) listItemView.findViewById(R.id.image_view);
viewHolder.switch1 = (Switch) listItemView.findViewById(R.id.switch_button);

final ViewHolder finalViewHolder = viewHolder;
viewHolder.switch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
position1 = (Integer)v.getTag();
finalViewHolder.switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
DatabaseReference databaseReference1=database.getReference("data").child(web[position1].replace("/",""));
DatabaseReference databaseReference2=databaseReference1.child("Switch");
if (isChecked){
databaseReference2.setValue("On");
}else {
databaseReference2.setValue("Off");
}
}
});
}
});

listItemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)listItemView.getTag();
}

viewHolder.switch1.setTag(position);
viewHolder.txtTitle.setText(web[position]);
viewHolder.imageView.setImageResource(imageId[position]);

return listItemView;
}

Answer Source

Because you are setting the OnCheckedChangeListener in the OnClickListener; so the onCheckChanged() method only gets called after the first click (not on the first click).

Just use an OnCheckedChangeListener from the start instead.

viewHolder.switch1 = (Switch) listItemView.findViewById(R.id.switch_button);

switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        position1 = (Integer) buttonView.getTag();

        DatabaseReference databaseReference1 = database.getReference("data").child(web[position1].replace("/",""));
        DatabaseReference databaseReference2 = databaseReference1.child("Switch");

        if (isChecked){
            databaseReference2.setValue("On");
        } else {
            databaseReference2.setValue("Off");
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download