问题
Im trying to retrieve data from Firebase database to my layout and i cant see any items of FirebaseRecyclerAdapter
in the layout, please help. i followed a tutorial that showed how to do that and when i run the app i doesn\'t see any items but i can scroll.
public class Home extends AppCompatActivity {
Button logout;
FirebaseAuth firebaseAuth;
private static final String Tag = \"Home\";
DatabaseReference dbUsers;
DatabaseReference db;
private RecyclerView mBlogList;
FirebaseAuth.AuthStateListener authStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
logout = (Button) findViewById(R.id.logout);
dbUsers = FirebaseDatabase.getInstance().getReference().child(\"Users\");
Log.d(Tag, \"onCreate: starting\");
firebaseAuth = FirebaseAuth.getInstance();
dbUsers.keepSynced(true);
db = FirebaseDatabase.getInstance().getReference().child(\"Blog\");
db.keepSynced(true);
mBlogList = (RecyclerView)findViewById(R.id.Blog_List);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
setUpNav();
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
Intent lgnIntent = new Intent(getApplicationContext(), MainActivity.class);
lgnIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(lgnIntent);
}
}
};
}
@Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(authStateListener);
checkUser();
FirebaseRecyclerOptions<Postzone> options =
new FirebaseRecyclerOptions.Builder<Postzone>()
.setQuery(db, Postzone.class)
.build();
FirebaseRecyclerAdapter<Postzone, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Postzone, PostViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull PostViewHolder holder, int position, @NonNull Postzone model) {
final String post_key = getRef(position).getKey().toString();
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
holder.setName(model.getUsername());
holder.setImage(getApplicationContext(),model.getImageUrl());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent singleActivity = new Intent(Home.this, SinglePostActivity.class);
singleActivity.putExtra(\"PostID\", post_key);
startActivity(singleActivity);
}
});
}
@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_row, parent, false);
return new PostViewHolder(view);
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder{
View mView;
public PostViewHolder(View itemView) {
super(itemView);
mView= itemView;
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setName(String name){
TextView post_user = (TextView) mView.findViewById(R.id.madeBy);
post_user.setText(name);
}
public void setDesc(String desc){
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image){
ImageView img = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(img);
}
}
private void checkUser() {
if (firebaseAuth.getCurrentUser() != null) {
final String user_id = firebaseAuth.getCurrentUser().getUid();
dbUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(user_id)) {
Intent main = new Intent(Home.this, Account_Setup.class);
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
private void setUpNav() {
Log.d(Tag, \"setUpNav: starting func.\");
BottomNavigationViewEx nav = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.setupBottomNav(nav);
BottomNavigationViewHelper.enableNav(Home.this, nav);
Menu menu = nav.getMenu();
MenuItem item = menu.getItem(0);
item.setChecked(true);
}
}
This is the Home.xml layout:
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
xmlns:app=\"http://schemas.android.com/apk/res-auto\"
xmlns:tools=\"http://schemas.android.com/tools\"
android:layout_width=\"match_parent\"
android:orientation=\"vertical\"
android:layout_height=\"match_parent\"
app:layout_behavior=\"@string/appbar_scrolling_view_behavior\"
tools:context=\"com.apps.amith.scj.Home\"
tools:showIn=\"@layout/activity_home\">
<RelativeLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\">
<android.support.v7.widget.RecyclerView
android:id=\"@+id/Blog_List\"
android:layout_width=\"match_parent\"
android:layout_margin=\"10dp\"
android:layout_height=\"match_parent\">
</android.support.v7.widget.RecyclerView>
<include layout=\"@layout/bottom_nav\"/>
</RelativeLayout>
This is the blog class:
public class Postzone {
private String title, desc, imageUrl, username, profImg;
public Postzone(String title, String desc, String imageUrl, String username, String profImg) {
this.title = title;
this.desc = desc;
this.profImg = profImg;
this.imageUrl=imageUrl;
this.username = username;
}
public Postzone() {
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public void setUsername(String username) {
this.username = username;
}
public void setTitle(String title) {
this.title = title;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImageUrl() {
return imageUrl;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public String getUsername() {
return username;
}
public String getProfImg() {
return profImg;
}
public void setProfImg(String profImg) {
this.profImg = profImg;
}
}
This is the blog row:
<android.support.v7.widget.CardView xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\"
android:layout_margin=\"20dp\">
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:orientation=\"vertical\">
<ImageView
android:id=\"@+id/post_image\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:adjustViewBounds=\"true\"
android:background=\"@drawable/def_profile\" />
<TextView
android:id=\"@+id/post_title\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"Post Text Goes Here\"
android:textSize=\"16dp\"
android:textStyle=\"bold\" />
<TextView
android:id=\"@+id/post_desc\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"The post Description Goes here\" />
<TextView
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"נוצר על ידי:\"
android:id=\"@+id/madeBy\"/>
</LinearLayout>
回答1:
The tutorial that you are following is wrong. To solve your problem, please consider following these steps.
Move all the code from the
onStart()
method insideonCreate()
method except these two lines of code:super.onStart(); firebaseAuth.addAuthStateListener(authStateListener);
Make your
firebaseRecyclerAdapter
varaible global:private FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter;
Remove
FirebaseRecyclerAdapter<Blog, BlogViewHolder>
from theonCreate()
method.Add the following lines of code in the
onStart()
andonStop()
methods.@Override protected void onStart() { super.onStart(); firebaseRecyclerAdapter.startListening(); } @Override protected void onStop() { super.onStop(); if(firebaseRecyclerAdapter != null) { firebaseRecyclerAdapter.stopListening(); } }
The most important thing is to remove the
static
keyword from your class declaration. Should be only:public class PostViewHolder extends RecyclerView.ViewHolder {}
来源:https://stackoverflow.com/questions/48846631/i-cant-see-any-firebaserecycleradapter-items-on-my-layout