如何使用FirebaseUI使用Firebase数据库中的数据填充RecyclerView?

时间:2018-11-15 22:26:40

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

我正在努力填充我的回收者视图,但无法确定原因。我试图用Firebase数据库中的数据填充视图,我可以成功获取数据,但是我不知道如何正确设置回收站视图,我也在使用FirebaseUI。这是我的活动,其中包含firebaseUI和数据库调用:

public class LoggedInActivity extends AppCompatActivity {

private DatabaseReference mDatabase;

RecyclerView contentView;

private TextView mTextMessage;
        ImageButton positive;
        ImageButton balanced;
        ImageButton negative;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);

                return true;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);

                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);

                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logged_in);

    positive = findViewById(R.id.heartButton);
    balanced = findViewById(R.id.balancedButton);
    negative = findViewById(R.id.negativeButton);

    mTextMessage = findViewById(R.id.message);
    mTextMessage.setTextColor(Color.WHITE);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    mDatabase = FirebaseDatabase.getInstance().getReference();

    DatabaseReference mRef = mDatabase.child("posts");
    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("posts")
            .limitToLast(50);

    query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
            Post newPost = dataSnapshot.getValue(Post.class);
            System.out.println("Title: " + newPost.title);
            System.out.println("Body: " + newPost.body);
            System.out.println("Previous Post ID: " + prevChildKey);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {}

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });


        FirebaseRecyclerOptions<Post> options =
            new FirebaseRecyclerOptions.Builder<Post>()
                    .setQuery(query, Post.class)
                    .build();

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Post, PostHolder>(options) {
        @Override
        public PostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.message for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.post_content, parent, false);

            return new PostHolder(view);
        }

        @Override
        protected void onBindViewHolder(PostHolder holder, int position, Post model) {
            // Bind the Chat object to the ChatHolder
            // ...
           holder.bind


        }
    };

    contentView = findViewById(R.id.recyclerView);
    contentView.setAdapter(adapter);

}

}

我还创建了用于从数据库检索帖子的类:

@IgnoreExtraProperties
public class Post {

public String title;
public String body;
public String username;

public Post() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public Post(String title, String body, String username) {
    title = title;
    body = body;
    username = username;
}

public String getTitle() { return title; }

public String getBody() { return body; }

public String getUsername() { return username; }

}

此刻,我只是想获得职位标题,我已经使这个自定义持有人成为了

public class PostHolder extends RecyclerView.ViewHolder {

TextView title;
TextView body;
TextView username;

public PostHolder(@NonNull View itemView) {
    super(itemView);
    title = (TextView) itemView.findViewById(R.id.title)
}

}

我认为这是我遇到的问题,我不确定如何绑定Post数据以使其显示在我创建为post_content.xml的此Label上:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"/>

 </LinearLayout>

我希望对这个系统进行解释,因为我发现它在习惯持有人,装订和充气部分上有些混乱。 我要去哪里错了?谢谢。

0 个答案:

没有答案