片段中的Android FirebaseRecyclerAdapter

时间:2017-06-08 04:31:36

标签: java android android-fragments

我使用Android Studio提供的类来进行使用带有ViewPager的ActionBar标签的标签式活动。在这项活动中,我试图初始化RecyclerView& CardView包含Firebase数据库中的数据。

问题:RecyclerView为空,如下所示。 [1]

空屏幕但滚动效果很好。 [2]

这是我的代码

public class Produce extends Fragment {

    private DatabaseReference mDatabase;
    private FirebaseRecyclerAdapter<ProduceSetting, ProduceViewHolder> mFirebaseAdapter;
    private RecyclerView mProduceList = null;
    private View rootView;

    private Context c;
    private LinearLayoutManager manager;

    public Produce() {}


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_produce, container, false);
        c = getContext();
        mProduceList = (RecyclerView) rootView.findViewById(R.id.produce_recycler_view);
        manager = new LinearLayoutManager(c);
        mProduceList.setHasFixedSize(true);

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Produce");

        mFirebaseAdapter = new FirebaseRecyclerAdapter<ProduceSetting, ProduceViewHolder>(

                ProduceSetting.class, R.layout.card_item_produce, ProduceViewHolder.class, mDatabase

        ) {
            @Override
            protected void populateViewHolder(ProduceViewHolder viewHolder, ProduceSetting model, int position) {
                viewHolder.setName(model.getGameName());
                viewHolder.setType(model.getGameType());
                viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
            }
        };


        mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                super.onItemRangeInserted(positionStart, itemCount);
                int friendlyMessageCount = mFirebaseAdapter.getItemCount();
                int lastVisiblePosition =
                        manager.findLastCompletelyVisibleItemPosition();
                // If the recycler view is initially being loaded or the
                // user is at the bottom of the list, scroll to the bottom
                // of the list to show the newly added message.
                if (lastVisiblePosition == -1 ||
                        (positionStart >= (friendlyMessageCount - 1) &&
                                lastVisiblePosition == (positionStart - 1))) {
                    mProduceList.scrollToPosition(positionStart);
                }
            }
        });

        mProduceList.setLayoutManager(manager);
        mProduceList.setAdapter(mFirebaseAdapter);



        return rootView;
    }



    public static class ProduceViewHolder extends RecyclerView.ViewHolder {

        View mView;
        public ProduceViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
        public void setName(String name) {
            TextView post_name = (TextView) mView.findViewById(R.id.post_name);
            post_name.setText(name);
        }
        public void setType(String type) {
            TextView post_type = (TextView) mView.findViewById(R.id.post_type);
            post_type.setText(type);
        }
        public void setImage(Context ctx, String image) {
            ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
            Picasso.with(ctx).load(image).into(post_image);
        }
    }
}

这是我的getter和setter

public class ProduceSetting {

    private String GameName;
    private String GameType;
    private String image;

    public ProduceSetting(){}

    public ProduceSetting(String gameName, String gameType, String image) {
        GameName = gameName;
        GameType = gameType;
        this.image = image;
    }

    public String getGameName() {
        return GameName;
    }

    public void setGameName(String gameName) {
        GameName = gameName;
    }

    public String getGameType() {
        return GameType;
    }

    public void setGameType(String gameType) {
        GameType = gameType;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

}
请帮助我。

1 个答案:

答案 0 :(得分:0)

我错了。

mDatabase = FirebaseDatabase.getInstance()。getReference()。child(“Produce”);

mDatabase = FirebaseDatabase.getInstance()。getReference()。child(“Product”);