我从Firebase数据库开始。我做了一个小的任务跟踪器应用程序,我能够将项目推送到数据库。我正在使用Firebase UI和FriebaseRecyclerView。
问题是我无法从firebaseRecyclerView更新子项单击。下面是我的视图持有人的代码。但是在复选框点击我的数据库没有按预期更新。
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());
}
}
});
} }
下面是我的适配器代码
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类
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
}
}
任何帮助都将非常感激。另外,我刚刚开始使用android,所以希望我能犯一些愚蠢的错误。
谢谢
达到目的的一个方法是:
1)当用户检查一个项目时,让push一个"特殊值"到firebase db.
2)在FirebaseRecyclerAdapter#populateViewHolder:如果项目有"特殊值",改变布局
试试google的例子:
https://github.com/firebase/quickstart-android/blob/master/database让我们看看星号按钮的实现。
在PostListFragment.java中你会发现:
// 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);
}
});
@Muhammad Hassan Niazi在if条件中使用push或setvalue方法更新数据库记录