我在布局上看不到任何 FirebaseRecyclerAdapter 项目 [英] I cant see any FirebaseRecyclerAdapter items on my layout

查看:19
本文介绍了我在布局上看不到任何 FirebaseRecyclerAdapter 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从 Firebase 数据库检索到我的布局,但我在布局中看不到 FirebaseRecyclerAdapter 的任何项目,请帮忙.我遵循了一个教程,该教程展示了如何做到这一点,当我运行该应用程序时,我看不到任何项目,但我可以滚动.

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);
}
}    

这是 Home.xml 布局:

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>

这是博客类:

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;
}
}

这是博客行:

<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>

推荐答案

您所遵循的教程是错误的.要解决您的问题,请考虑执行以下步骤.

The tutorial that you are following is wrong. To solve your problem, please consider following these steps.

  1. onStart() 方法中的所有代码移到 onCreate() 方法中,除了这两行代码:

  1. Move all the code from the onStart() method inside onCreate() method except these two lines of code:

super.onStart();
firebaseAuth.addAuthStateListener(authStateListener);

  • 使您的 firebaseRecyclerAdapter 成为全局变量:

    private FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter;
    

  • onCreate() 方法中移除 FirebaseRecyclerAdapter.

    onStart()onStop() 方法中添加以下代码行.

    Add the following lines of code in the onStart() and onStop() methods.

    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if(firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
    

  • 最重要的是从类声明中删除 static 关键字.应该只是:

  • The most important thing is to remove the static keyword from your class declaration. Should be only:

    public class PostViewHolder extends RecyclerView.ViewHolder {}
    

  • 这篇关于我在布局上看不到任何 FirebaseRecyclerAdapter 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆