Reputation: 330
I am starting with the Firebase Database. I have made a small Task Tracker application in which I am able to push items to DB. I am using Firebase UI and FriebaseRecyclerView.
The issue is I am unable to update childs on item clicks, from the firebaseRecyclerView. Below is the code for my view holder. But on checkbox clicks my DB is not updated as expected.
public static class TaskViewHolder extends RecyclerView.ViewHolder{
TextView title,description;
CheckBox checkBox;
FirebaseAuth mAuth;
FirebaseUser firebaseUser;
public TaskViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
description= (TextView) v.findViewById(R.id.Description);
checkBox = (CheckBox) v.findViewById(R.id.checkboxTaskRow);
mAuth = FirebaseAuth.getInstance();
firebaseUser = mAuth.getCurrentUser();
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.taskList);
final FirebaseRecyclerAdapter firebaseRecyclerAdapter = (FirebaseRecyclerAdapter) recyclerView.getAdapter();
if(checkBox.isChecked()) {
SpannableString content = new SpannableString(title.getText());
content.setSpan(new StrikethroughSpan(), 0, content.length(), 0);
title.setText(content);
content = new SpannableString(description.getText());
content.setSpan(new StrikethroughSpan(), 0, content.length(), 0);
description.setText(content);
}
else
{
description.setText(description.getText().toString());
title.setText(title.getText().toString());
}
}
});
} }
Also Below is my adapter code
final FirebaseRecyclerAdapter<UnitTask,TaskViewHolder> adapter =
new FirebaseRecyclerAdapter<UnitTask, TaskViewHolder>(UnitTask.class,R.layout.task,TaskViewHolder.class,userRef) {
@Override
protected void populateViewHolder(TaskViewHolder viewHolder, UnitTask model, int position) {
mAuth = FirebaseAuth.getInstance();
firebaseUser = mAuth.getCurrentUser();
viewHolder.title.setText(model.getTitle());
viewHolder.description.setText(model.getDescription());
viewHolder.checkBox.setChecked(model.isCompleted());
}
};
My UnitTask class
public class UnitTask {
public String title;
public String description;
boolean completed;
public UnitTask(String title, String description, boolean completed) {
this.title = title;
this.description = description;
this.completed = completed;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
public UnitTask() {
// Do Nothing
}
}
Any help will be much appreciated. Also I am just starting with android so do expect of me to make silly mistakes.
Thanks
Upvotes: 0
Views: 343
Reputation: 2495
A work around to get your purpose is:
1) when user check on an item, let push a "special value" to firebase db.
2) In FirebaseRecyclerAdapter#populateViewHolder:if item has "special value", change the layout
UPDATE
Try the example from google:
https://github.com/firebase/quickstart-android/blob/master/database
Let see the implementation of Star button.
in PostListFragment.java you will find:
// Determine if the current user has liked this post and set UI accordingly
if (model.stars.containsKey(getUid())) {
viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_24);
} else {
viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_outline_24);
}
// Bind Post to ViewHolder, setting OnClickListener for the star button
viewHolder.bindToPost(model, new View.OnClickListener() {
@Override
public void onClick(View starView) {
// Need to write to both places the post is stored
DatabaseReference globalPostRef = mDatabase.child("posts").child(postRef.getKey());
DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postRef.getKey());
// Run two transactions
onStarClicked(globalPostRef);
onStarClicked(userPostRef);
}
});
Upvotes: 1
Reputation: 384
@Muhammad Hassan Niazi update your database record inside that if condition either using push or setvalue method
Upvotes: 0