如何解决错误:“ E / RecyclerView:未连接适配器;正在跳过布局”

时间:2019-02-15 16:27:06

标签: android firebase recyclerview-layout

我已经看到许多不同的解决方案,并且已经实现了很多解决方案,但是我仍然无法解决我的问题“ E / RecyclerView:未连接适配器;跳过布局”。我正在尝试从Firebase检索图像,并将它们放入片段中。

我的图片片段:

public class ImagesFragment extends Fragment {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private LinearLayoutManager mLinearLayoutManager;

private DatabaseReference mDatabaseRef;
private List<OnlineUpload> mUploads;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstance) {
    super.onViewCreated(view, savedInstance);

    mAdapter = new ImageAdapter(getActivity(), mUploads);
}


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

    mLinearLayoutManager = new LinearLayoutManager(getActivity());
    mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    mRecyclerView = view.findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);
    mRecyclerView.setAdapter(mAdapter);

    mUploads = new ArrayList<>();

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

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                OnlineUpload upload = postSnapshot.getValue(OnlineUpload.class);
                mUploads.add(upload);
            }

            mAdapter = new ImageAdapter(getActivity(), mUploads);

            mRecyclerView.setAdapter(mAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            showMessage(databaseError.getMessage());
        }
    });

    return view;

}

private void showMessage(String s) {
    Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
}
}

我的图像适配器:

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

// To get values from the outside, to our adapter we need this constructor
public ImageAdapter(Context context, List<OnlineUpload> uploads){
    mContext = context;
    mUploads = uploads;
}

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

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int i) {
    OnlineUpload uploadCurrent = mUploads.get(i);
    holder.textViewName.setText(uploadCurrent.getName());
    Picasso.with(mContext)
            .load(uploadCurrent.getImageUrl())
            .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);
    }
}
}

我的OnlineUpload类:

public class OnlineUpload {
private String mName;
private String mImageUrl;

public OnlineUpload(){
    //empty constructor
}

public OnlineUpload(String name, String imageUrl){
    if (name.trim().equals("")){
        name = "Name me!";
    }
}

public String getName(){
    return mName;
}

public void setName(String name){
    mName = name;
}

public String getImageUrl(){
    return mImageUrl;
}

public void setImageUrl(String imageUrl){
    mImageUrl = imageUrl;
}
}

0 个答案:

没有答案