RecyclerView:未连接适配器;跳过布局无数据显示

时间:2018-11-19 02:13:35

标签: android android-cardview

它没有错误,但是当我运行该支架时,它没有显示,当我查看日志猫时,出现了一个RecyclerView错误:没有连接适配器;跳过布局。这是我的代码;我已经在我的recyclerview中添加了布局管理器,但是它仍然显示没有附加的适配器跳过布局。

这是我的片段类和适配器类代码供参考。请让我知道是什么问题。

public class dogs extends Fragment 
{
    private RecyclerView recyclerView;
    private PostAdapter postAdapter;
    private List<Model> ModelList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dogs, container, false);


        recyclerView = view.findViewById(R.id.Feeddogs);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setStackFromEnd(true);
        linearLayoutManager.setReverseLayout(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        ModelList = new ArrayList<>();
        postAdapter = new PostAdapter(getContext(),ModelList);
        recyclerView.setAdapter(postAdapter);


      showdata();


        return view;
    }

    private void showdata() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Dogs");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                ModelList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Model model = snapshot.getValue(Model.class);
                    ModelList.add(model);

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

这是我的适配器代码...

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

    public Context mContext;
    public List<Model> mPost;

    private FirebaseUser firebaseUser;

    public PostAdapter(Context mContext, List<Model> mPost) {
        this.mContext = mContext;
        this.mPost = mPost;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.row, parent,false);
        return new PostAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        Model model = mPost.get(position);
        Glide.with(mContext).load(model.getPetpicture()).into(holder.postimage);
        holder.postname.setText(model.getPetname());
        holder.postbreed.setText(model.getBreed());
        holder.postage.setText(model.getAge());
        holder.postsize.setText(model.getPetsize());
        holder.postcolor.setText(model.getPetcolor());
        holder.postreason.setText(model.getReason());
        ownerinfo( holder.postby,holder.posterimage,model.getUserid());

    }

    @Override
    public int getItemCount() {
        return mPost.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public CircleImageView posterimage;
        public ImageView postimage;
        public Button like,adoptnme;
        public TextView postby,postname,postbreed,postage,postsize,postcolor,postreason,likecount;

        public ViewHolder(View itemView) {
            super(itemView);




            posterimage = itemView.findViewById(R.id.posterimage);
            postby = itemView.findViewById(R.id.postby);

            postimage = itemView.findViewById(R.id.postimage);
            like = itemView.findViewById(R.id.like);
            likecount = itemView.findViewById(R.id.likecount);
            adoptnme = itemView.findViewById(R.id.adoptme);
            postname = itemView.findViewById(R.id.postname);
            postbreed = itemView.findViewById(R.id.postbreed);
            postage = itemView.findViewById(R.id.postage);
            postsize = itemView.findViewById(R.id.postsize);
            postcolor = itemView.findViewById(R.id.postcolor);
            postreason = itemView.findViewById(R.id.postreason);

        }

    }

    private void ownerinfo(final TextView postby, final CircleImageView posterimage, String userid)
    {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("User").child(userid);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UsersHappyPaws usersHappyPaws = dataSnapshot.getValue(UsersHappyPaws.class);
                Glide.with(mContext).load(usersHappyPaws.getProfilepic()).into(posterimage);
                postby.setText(usersHappyPaws.getName());

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

您需要在RecyclerView上调用notifyDataSetChanged,因为您的数据是异步接收的:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    ModelList.clear();
    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
        Model model = snapshot.getValue(Model.class);
        ModelList.add(model);
    }
    recyclerView.notifyDataSetChanged();
}

至于错误,仅是警告。但是,如果您担心它,可以先设置setAdapter,然后再设置setLayoutManager,它必须已触发以在没有适配器的情况下分派布局更新,因此这就是您看到警告的原因。 / p>