Reputation: 21
When i have mulitple switch on a android recycleview the latest one is giving full functionality. Meaning if the last one is check or unchecked it changes the value on a firebase database. the others however are will not change the value when checked or unchecked.
I am not sure how to fix this so that any checked or unchecked will change the value of the corresponeding value on the firebase database.
this is my code for the switch in my RecyclerViewAdapter.
holder.mode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mode.isChecked()){
x.blindsMode("auto",x.blindkey);
data.putString("mode"+x.blindkey,"auto");
//data.commit();
Toast.makeText(view.getContext(),"Blind set to automatic mode", Toast.LENGTH_SHORT).show();
}else {
x.blindsMode("man",x.blindkey);
data.putString("mode"+x.blindkey,"man");
//data.commit();
Toast.makeText(view.getContext(),"Blind set to manual mode", Toast.LENGTH_SHORT).show();
}
data.commit();
}
});
this is the class where i am setting the value when checked on a firebase database.
public class HomeBlinds {
DatabaseReference ref;
String blindkey;
// this will house data on the specific blind.
public HomeBlinds(String blindkey) {
this.blindkey = blindkey;
ref = FirebaseDatabase.getInstance().getReference(blindkey);
}
//when this method is called it will set the status node of the blind to open which is then read
// by the device
public void openBlinds(){
ref.child("Status").setValue("open");
}
public void closeBlinds(){
ref.child("Status").setValue("close");
}
public void blindsMode(String mode,String bk){
DatabaseReference r = FirebaseDatabase.getInstance().getReference(bk);
r.child("Mode").setValue(mode);
//ref.child("Mode").setValue(mode);
}
This is what my firebase database looks like, I am changing the Mode key to either auto when the switch is check or man when not checked.
this is my entire holder class
@Override
public void onBindViewHolder(@NonNull HomeViewHolder holder, int position) {
HomeBlinds x = testblinds.get(position);
String bkey = x.blindkey;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(bkey);
ref.child("Location").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String lo = snapshot.getValue(String.class);
holder.loc.setText(context.getString(R.string.loc)+" "+ lo);
location = lo;
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w("Temp error",error.toException());
}
});
ref.child("Temperature").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String t = snapshot.getValue(String.class);
holder.temp.setText(context.getString(R.string.temp)+" "+ t);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w("Temp error",error.toException());
}
});
ref.child("Light").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String li = snapshot.getValue(String.class);
holder.light.setText(context.getString(R.string.li)+" "+ li);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w("Temp error",error.toException());
}
});
//when open button is pressed status value on firebase = open
holder.open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
x.openBlinds();
Toast.makeText(view.getContext(),view.getContext().getString(R.string.open) + location, Toast.LENGTH_SHORT).show();
}
});
//when close button is pressed status value on firebase = close
holder.close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
x.closeBlinds();
Toast.makeText(view.getContext(), view.getContext().getString(R.string.closing) + location, Toast.LENGTH_SHORT).show();
}
});
SharedPreferences sharedPreferences = context.getSharedPreferences("saved", Context.MODE_PRIVATE);
SharedPreferences.Editor data = sharedPreferences.edit();
holder.mode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mode.isChecked()){
x.blindsMode("auto",x.blindkey);
data.putString("mode"+x.blindkey,"auto");
//data.commit();
Toast.makeText(view.getContext(),"Blind set to automatic mode", Toast.LENGTH_SHORT).show();
}else {
x.blindsMode("man",x.blindkey);
data.putString("mode"+x.blindkey,"man");
//data.commit();
Toast.makeText(view.getContext(),"Blind set to manual mode", Toast.LENGTH_SHORT).show();
}
data.commit();
}
});
}
I have tired assinging the specific blind key to the database reference, but that has not worked. I have also tired setOnCheckChangedListener for the switch but that has not worked either.
Upvotes: 1
Views: 22