E / RecyclerView:未连接适配器;跳过布局NEW

时间:2018-11-13 17:29:07

标签: java android android-recyclerview

我查看了许多解决方案,并根据我以前的代码版本的反馈进行了调整,但是我仍然无法正常工作并且出现相同的错误

JudgeActivty:

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

    RecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    RecyclerView.setHasFixedSize(true);
    RecyclerView.setLayoutManager(new LinearLayoutManager(this));

    ProgressCircle = findViewById(R.id.progress_circle);

    mUploads = new ArrayList<>();

    DatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

    DatabaseRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                UploadClass upload =
                        postSnapshot.getValue(UploadClass.class);
                mUploads.add(upload);
            }
            Adapter = new ImageAdapter(JudgeActivity.this, mUploads);
            ProgressCircle.setVisibility(View.INVISIBLE);
            RecyclerView.setAdapter(Adapter);
        }
    });
}

适配器:

public class ImageAdapter extends
        RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
    private Context mContext;
    private List<UploadClass> mUploads;

    public ImageAdapter(Context context, List<UploadClass> uploads) {
        mContext = context;
        mUploads = uploads;
    }


    @Override
    public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.image_item,
                parent, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ImageViewHolder holder, int position) {
        UploadClass uploadCurrent = mUploads.get(position);
        holder.textViewName.setText(uploadCurrent.getName());
        Picasso.with(mContext)
                .load(uploadCurrent.getImageUrl())
                .placeholder(R.mipmap.ic_launcher)
                .fit()
                .centerCrop()
                .into(holder.imageView);

    }


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

    public class ImageViewHolder extends RecyclerView.ViewHolder {
        public TextView textViewName;
        public ImageView imageView;

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

            textViewName = itemView.findViewById(R.id.text_view_name);
            imageView = itemView.findViewById(R.id.image_view_upload);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

实际上有问题吗? “错误”不是错误,只是警告。之所以这样显示是因为,当首次显示“活动”时,RecyclerView尚未设置任何适配器。您只能在Firebase值更改时设置适配器,这是个坏主意。

您应该在ValueEventListener之外创建并设置适配器:

RecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
RecyclerView.setHasFixedSize(true);
RecyclerView.setLayoutManager(new LinearLayoutManager(this));
Adapter = new ImageAdapter(JudgeActivity.this, mUploads);
RecyclerView.setAdapter(Adapter);

此外,在Java中,变量名紧跟camelCase,而不是TitleCase。您不必遵循该准则,但是在调试代码时,它可以减少其他人的困惑。