我正在开发一个应用程序,需要从firebase获取数据到回收器视图,但它没有这样做。 应用程序运行正常,但在回收站视图部分中没有显示任何内容。
MainArea.java(的onCreate)
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_rating);
Email=(TextView)findViewById(R.id.Email);
Fullname=(TextView)findViewById(R.id.Name);
City=(TextView)findViewById(R.id.City);
Address=(TextView)findViewById(R.id.Address);
rating=(TextView)findViewById(R.id.Rating);
int s=getIntent().getIntExtra("position",0);
DatabaseReference ref=MainArea.FBRA.getRef(s);
ss=ref.getKey();
DatabaseReference ref2= FirebaseDatabase.getInstance().getReference().child("Users").child(ss);
ref2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange( DataSnapshot dataSnapshot ) {
email=dataSnapshot.child("email").getValue().toString();
fullname=dataSnapshot.child("fullname").getValue().toString();
city=dataSnapshot.child("city").getValue().toString();
address=dataSnapshot.child("address").getValue().toString();
Totalrating=dataSnapshot.child("TotalRating").getValue().toString();
rating.setText(Totalrating);
Email.setText(email);
Fullname.setText(fullname);
City.setText(city);
Address.setText(address);
}
@Override
public void onCancelled( DatabaseError databaseError ) {
}
});
myRef2=FirebaseDatabase.getInstance().getReference("RatingPosts").child(ss);
Rview=(RecyclerView)findViewById(R.id.recycler);
Rview.setHasFixedSize(true);
Rview.setLayoutManager(new LinearLayoutManager(DisplayRating.this));
FirebaseRecyclerAdapter<dataget,ViewHolder2> Fbra=new FirebaseRecyclerAdapter<dataget, ViewHolder2>(
dataget.class,
R.layout.cardr2,
ViewHolder2.class,
myRef2
) {
@Override
protected void populateViewHolder( ViewHolder2 viewHolder, dataget model, int position ) {
viewHolder.setAddress(model.getAddress());
viewHolder.setPicture(model.getPicture());
viewHolder.setRating(model.getRating());
}
};
Rview.setAdapter(Fbra);
}
ViewHolder
public static class ViewHolder2 extends RecyclerView.ViewHolder
{
View mView;
Context ctx;
public ViewHolder2( View itemView ) {
super(itemView);
mView=itemView;
ctx=itemView.getContext();
}
public void setAddress( String address ) {
TextView Add=(TextView)mView.findViewById(R.id.add);
Add.setText(address);
}
public void setPicture( String picture ) {
ImageView img=(ImageView)mView.findViewById(R.id.img);
Picasso.with(ctx).load(picture).into(img);
}
public void setRating( String rating ) {
TextView rate=(TextView)mView.findViewById(R.id.ratee);
rate.setText(rating);
}
}
dataget.java
package com.hitachiapp.hitachi.hitachiapp;
/ ** *由Mohit Gupta于23-05-2018创建。 * /
public class dataget {
String Address,Picture,Rating;
public dataget( String address, String picture, String rating ) {
Address = address;
Picture = picture;
Rating = rating;
}
public dataget() {
}
public String getAddress() {
return Address;
}
public void setAddress( String address ) {
Address = address;
}
public String getPicture() {
return Picture;
}
public void setPicture( String picture ) {
Picture = picture;
}
public String getRating() {
return Rating;
}
public void setRating( String rating ) {
Rating = rating;
}
}
firebase结构 this is the image
答案 0 :(得分:0)
1 - 确保在清单文件中添加权限行
<uses-permission android:name="android.permission.INTERNET" />
2 - 检查DatabaseReference
。尝试使用FirebaseDatabase.getInstance().getReference("RatingPosts")
确认链接是否有效或在logcat中记录数据
3 - 确保您的数据模型是您的数据库数据模型。 (您的屏幕截图表明孩子们有更多孩子,而您的数据模型显示数据类型)
4 - 确保在适配器
之后调用recycler.setAdapter(adapter);
5 - 确保在号码4之前拨打recycler.setLayoutManager(layout);
您的代码不足以在没有日志的情况下进行调试,但这是人们在使用firebase时常犯的错误。