【发布时间】:2018-07-11 07:02:00
【问题描述】:
我需要在回收器视图中从 AGS Biryani 获取畅销书值,不知道如何基于父节点将子值添加到回收器适配器
型号
public class Food_List {
private String itemname;
private String itemdescrp;
private long itemnewprice;
private long itemoldprice;
public String getItemname() {
return itemname;
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public String getItemdescrp() {
return itemdescrp;
}
public void setItemdescrp(String itemdescrp) {
this.itemdescrp = itemdescrp;
}
public long getItemnewprice() {
return itemnewprice;
}
public void setItemnewprice(long itemnewprice) {
this.itemnewprice = itemnewprice;
}
public long getItemoldprice() {
return itemoldprice;
}
public void setItemoldprice(long itemoldprice) {
this.itemoldprice = itemoldprice;
}
}
Food_List_ViewHolders
ViewHolders 在 Recyler 视图中添加值
public class Food_List_ViewHolders extends RecyclerView.ViewHolder {
View mView;
private Context context;
Context mContext;
String nodata;
public Food_List_ViewHolders(View itemView) {
super(itemView);
mView=itemView;
}
@SuppressLint("SetTextI18n")
public void setDetails(Context applicationContext, final String itemname, String itemdescrp,
final long itemnewprice,
final long itemoldprice
)
{
final TextView dishitemname=mView.findViewById(R.id.dishheader);
TextView dishitemnamedescrp=mView.findViewById(R.id.dishheaderdescrp);
TextView dishitemnameoldprice=mView.findViewById(R.id.itemoldprice);
TextView dishitemnamenewprice=mView.findViewById(R.id.itemnewprice);
dishitemname.setText(itemname);
dishitemnamedescrp.setText(itemdescrp);
dishitemnamenewprice.setText(applicationContext.getString(R.string.Rup) + itemnewprice);
dishitemnameoldprice.setText(applicationContext.getString(R.string.Rup) + itemoldprice);
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View mView) {
Context context = mView.getContext();
}
});
}
}
主要活动
不知道如何在 DataSnapshot 中添加子值
mRecycleriew =findViewById(R.id.my_recycler_views);
mRecycleriew.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase= FirebaseDatabase.getInstance();
mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
//DatabaseReference restaurantsRef = mRef.child("restaurants");
mRef(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
progressDoalog.dismiss();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void mRef(ValueEventListener valueEventListener) {
}
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
Food_List.class,
R.layout.item_child,
Food_List_ViewHolders.class,
mRef)
{
@Override
protected void populateViewHolder(Food_List_ViewHolders viewHolder, Food_List model, int position) {
viewHolder.setDetails(getApplicationContext(),model.getItemname(),model.getItemdescrp(),model.getItemnewprice(),model.getItemoldprice());
}
};
mRecycleriew.setAdapter(firebaseRecyclerAdapter);
}
【问题讨论】:
-
您无法添加到 shapshot。您只能从 snapshop 获得。如果您需要将值发送到火基,您可以使用 .setValue() 到节点。
-
您可以迭代快照以获取当前孩子下的所有孩子。使用 for 循环,例如for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) { String name = (String) messageSnapshot.child("item").getValue(); String price= (String) messageSnapshot.child("price").getValue(); }
-
谢谢@JawadZeb 你能分享代码吗???
-
我会重写你的代码。告诉我你想做什么?您想获取餐厅节点下的所有值吗?
-
@JawadZeb 看起来 Kali 正在使用 FirebaseUI 的
FirebaseRecyclerAdapter,它已经处理了你提到的循环。他的populateViewHolder有一个模型项Food_List,FirebaseUI 会自动从DataSnapshot反序列化。我(还)不确定为什么它不工作,但他们不应该自己循环getChildren()。
标签: android firebase firebase-realtime-database firebaseui