FireBaseRecyclerAdapter问题

FireBaseRecyclerAdapter问题

本文介绍了FireBaseRecyclerAdapter问题.为什么setQuery告诉它无法使用引用和类解析方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我为您的片段粘贴了两个代码+ Posts.class有人知道为什么这会向我显示错误吗?我对此并不陌生,所以我真的无法自己弄清楚一些帮助会是非常棒的:)

Here I pasted you both codes for my fragment + the Posts.class does anyone know why this shows me an error ? I am new to this so I really cannot figure it out by myself some help would be great :)

FirebaseRecyclerOptions<Posts> option =
    new FirebaseRecyclerOptions.Builder<Posts>()
            .setQuery(postsRef, Posts.class).build();

片段代码

package com.LiFit;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

import de.hdodenhof.circleimageview.CircleImageView;

public class HomeFragment extends Fragment {

    View myFragment;
    private ImageButton postButton;
    private RecyclerView postlist;
    CollectionReference postsRef;
    FirebaseFirestore db;


    public static ProfileFragment getInstance() {
        return new ProfileFragment();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myFragment = inflater.inflate(R.layout.fragment_home, container, false);
        postButton = myFragment.findViewById( R.id.postButton);
        postlist = myFragment.findViewById(R.id.postlist);
        postlist.setHasFixedSize(true);
        LinearLayoutManager lmManager = new LinearLayoutManager(getActivity());
        lmManager.setReverseLayout(true);
        lmManager.setStackFromEnd(true);
        postlist.setLayoutManager(lmManager);
        db = FirebaseFirestore.getInstance();
        postsRef = db.collection("posts");
        postButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendUserToPostActivity();
            }
        } );

        DisplayAllUsersPosts();
        return myFragment;
    }

    private void DisplayAllUsersPosts() {
        FirebaseRecyclerOptions<Posts> option = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(postsRef, Posts.class).build();

        FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(option) {
            @Override
            protected void onBindViewHolder(@NonNull PostsViewHolder holder, int position, @NonNull Posts model) {

            }

            @NonNull
            @Override
            public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts_layout, parent, false);
                PostsViewHolder viewHolder = new PostsViewHolder(view);
                return viewHolder;
            }
        };
    }

    public static class PostsViewHolder extends RecyclerView.ViewHolder{
        View mView;
        TextView username, postTime, postDescription;
        CircleImageView profileImage;
        ImageView postImage;
        public PostsViewHolder(@NonNull View itemView) {
            super(itemView);
            mView=itemView;
            username = mView.findViewById(R.id.post_userName);
            postTime = mView.findViewById(R.id.post_uploadTime);
            postDescription = mView.findViewById(R.id.post_description);
            profileImage = mView.findViewById(R.id.post_profileImage);
            postImage = mView.findViewById(R.id.post_image);
        }
    }

    private void SendUserToPostActivity() {
        Intent postIntent = new Intent(getActivity(), PostActivity.class);
        startActivity(postIntent);
    }
}

Posts.class

Posts.class

package com.LiFit;

public class Posts {
    public String description, picture, postType, profileImage, time, uid, username;

    public Posts(){

    }

    public Posts(String description, String picture, String postType, String profileImage, String time, String uid, String username) {
        this.description = description;
        this.picture = picture;
        this.postType = postType;
        this.profileImage = profileImage;
        this.time = time;
        this.uid = uid;
        this.username = username;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getPostType() {
        return postType;
    }

    public void setPostType(String postType) {
        this.postType = postType;
    }

    public String getProfileImage() {
        return profileImage;
    }

    public void setProfileImage(String profileImage) {
        this.profileImage = profileImage;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

推荐答案

我可能遇到了与您相同的问题.您使用的是Firestore,而不是实时数据库,对不对?我用以下方法修复了它.

I probably encountered the same problem as you.You're using is a Firestore, not a Realtime Database, right?I fixed it with the following.

app build.gradle

app build.gradle

// before
implementation 'com.firebaseui:firebase-ui-database: 6.2.1'

// after
implementation 'com.firebaseui:firebase-ui-firestore:6.2.1'

来源:

// before
import com.firebase.ui.database.FirebaseRecyclerOptions;

// after
import com.firebase.ui.firestore.FirestoreRecyclerOptions;

这篇关于FireBaseRecyclerAdapter问题.为什么setQuery告诉它无法使用引用和类解析方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:15