我从Firebase数据库开始。我已经制作了一个小型的Task Tracker应用程序,我可以将项目推送到DB。我正在使用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());
}
};
我的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,所以请期待我犯下愚蠢的错误。
谢谢
答案 0 :(得分:1)
实现目标的工作是:
1)当用户检查某个项目时,请按一下&#34;特殊值&#34;到firebase db。
2)在FirebaseRecyclerAdapter中#populateViewHolder:如果项目具有&#34;特殊值&#34;,则更改布局
<强>更新强>
尝试使用google:
中的示例https://github.com/firebase/quickstart-android/blob/master/database
让我们看一下Star按钮的实现。
在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);
}
});
答案 1 :(得分:0)
@Muhammad Hassan Niazi使用push或setvalue方法在if条件中更新你的数据库记录