我的布局上看不到任何FirebaseRecyclerAdapter项目

时间:2018-02-17 22:40:33

标签: java android firebase firebase-realtime-database android-cardview

我正在尝试从Firebase数据库中检索数据到我的布局,并且我无法在布局中看到FirebaseRecyclerAdapter的任何项目,请帮忙。我按照教程展示了如何做到这一点,当我运行应用程序时,我没有看到任何项目,但我可以滚动。

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布局:

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

1 个答案:

答案 0 :(得分:0)

您关注的教程是错误的。要解决您的问题,请考虑执行以下步骤。

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

    super.onStart();
    firebaseAuth.addAuthStateListener(authStateListener);
    
  2. 制作firebaseRecyclerAdapter可变全球:

    private FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter;
    
  3. FirebaseRecyclerAdapter<Blog, BlogViewHolder>方法移除onCreate()

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

    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if(firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
    
  5. 最重要的是从类声明中删除 static 关键字。应该只是:

    public class PostViewHolder extends RecyclerView.ViewHolder {}