我一直试图让这个工作超过一个星期,但我仍然遇到同样的问题。我尝试使用点击监听器(即我的recyclerview中)专门更新子pickup
值。
我知道我需要从Firebase查询密钥,以及我不知道该怎么做的部分。然后将值设置为我将在.setValue()
这是我的数据库树:
Chat
-KgR30muFg-X6xmrqaXA
address: "123 ST"
confirm: ""
description: "Destin: Customer Name :Jhon"
phone: "8678876678"
pickup: ""
title: "oliver2jason@gmail.com 02:54:44"
-KgR34XayCVHu2Lveg62
address: "125 ST"
confirm: ""
description: "Miramar: Customer Name :Nathan "
phone: "929292929"
pickup: ""
title: "oliver2jason@gmail.com 02:55:19"
这是我的代码
public class Home extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference myref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myref= FirebaseDatabase.getInstance().getReference().child("Chat");
FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(
Blog.class,
R.layout.individual_row,
BlogViewHolder.class,
myref
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription(model.getDescription());
viewHolder.setAddress(model.getAddress());
viewHolder.setPhone(model.getPhone());
viewHolder.setPickup(model.getPickup());
viewHolder.setConfirm(model.getConfirm());
}
};
recyclerView.setAdapter(recyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView textView_title;
TextView textView_decription;
TextView textView_address;
TextView textView_phone;
TextView textView_pickup;
TextView textView_confirm;
Button bconfirm;
Button bpickup;
public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
textView_title = (TextView)itemView.findViewById(R.id.title);
textView_decription = (TextView) itemView.findViewById(R.id.description);
textView_address = (TextView) itemView.findViewById(address);
textView_phone = (TextView) itemView.findViewById(phone);
textView_pickup = (TextView) itemView.findViewById(pickup);
textView_confirm = (TextView) itemView.findViewById(R.id.confirm);
bconfirm = (Button) itemView.findViewById(R.id.bcomfirm);
bpickup = (Button) itemView.findViewById(R.id.bpickup);
}
public void setTitle(String title)
{
textView_title.setText(title+"");
}
public void setDescription(String description)
{
textView_decription.setText(description);
}
public void setAddress(String address) {
textView_address.setText(address);
}
public void setPhone(String phone) {
textView_phone.setText(phone);
}
public void setPickup(String pickup) {
textView_pickup.setText(pickup);
}
public void setConfirm(String confirm) {
textView_confirm.setText(confirm);
}
}
}
答案 0 :(得分:0)
首先,您必须获取要更新的记录的密钥,然后为将更新拾取值的按钮编写onClick方法:
FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(
Blog.class,
R.layout.individual_row,
BlogViewHolder.class,
myref){
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription(model.getDescription());
viewHolder.setAddress(model.getAddress());
viewHolder.setPhone(model.getPhone());
viewHolder.setPickup(model.getPickup());
viewHolder.setConfirm(model.getConfirm());
//Get Record Key
final String key = this.getRef(position).getKey();
//Update the Record
viewHolder.bpickup.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
myref.child(key).child("pickup").setValue("yourValue");
}
};
}
};
recyclerView.setAdapter(recyclerAdapter);