使用firebaseUI不会重试数据

时间:2018-02-22 09:34:08

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

我正在关注我尝试使用FirebaseUI检索数据的YouTube视频,但我无法在recyclerview中显示任何数据。

这是代码

public class MemeFragment extends Fragment {
    RecyclerView recyclerView;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference myref;
    FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder> firebaseRecyclerAdapter;
    FirebaseRecyclerOptions<Datafileinfo> options;

    public MemeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_meme, container, false);
        firebaseDatabase = FirebaseDatabase.getInstance();
        myref = FirebaseDatabase.getInstance().getReference("memes");
        recyclerView = (RecyclerView) view.findViewById(R.id.recyc);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        options = new FirebaseRecyclerOptions.Builder<Datafileinfo>()
                        .setQuery(myref, Datafileinfo.class)
                        .build();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder>
                (options) {
            @Override
            protected void onBindViewHolder(@NonNull ShowDataViewHolder holder, int position, @NonNull Datafileinfo model) {
                holder.ImageTitle(model.getTitle());
                holder.ImageUrl(model.getImage());
            }

            @Override
            public ShowDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custo, parent, false);
                return new ShowDataViewHolder(view);
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public ShowDataViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }

        private void ImageTitle(String title) {
            textView.setText(title);
        }

        private void ImageUrl(String url) {
            Glide.with(itemView.getContext()).
                    load(url).
                    into(imageView);
        }
    }
}

和datafileinfo是getter和setter类我也获得了Internet权限。注意我能从firebase获取单个值但是对于多个值,我遇到的问题是这是数据库。 在哪里我做错了任何想法??

注意我已经按照并完成了所有教程和类似的帖子。 database

1 个答案:

答案 0 :(得分:0)

为ViewHolder创建单独的类

    public class ShowDataViewHolder extends RecyclerView.ViewHolder{

    private ImageView imageView;
    private TextView textView;

    public ShowDataViewHolder (View itemView) {
        super(itemView);
        imageView = (ImageView)itemView.findViewById(R.id.imageView);
        textView = (TextView) itemView.findViewById(R.id.textView);
    }
 }

在您的MenuFragment中添加

    DatabaseReference mDatabaseMemes= //your reference to memes node

    FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder> adapter= new FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder>(yourModel.class, R.layout.yourLayout, 
    ShowDataViewHolder.class,mDatabaseMemes) {
        @Override
        protected void populateViewHolder(ShowDataViewHolder viewHolder, yourModel model, int position) {
            viewHolder.textView.setText(model.getTitle());
            Glide.with(context)
                    .load(model.getImage())
                    .fitCenter()
                    .into(viewHolder.imageView);

        }
    };
    recyclerView.setAdapter(adapter);
}

确认您的模型类的字符串标题和图像与模因节点下的子名称相同。