gpt4 book ai didi

java - fragment -E/RecyclerView : No adapter attached; skipping layout

转载 作者:行者123 更新时间:2023-12-02 04:56:01 24 4
gpt4 key购买 nike

我几乎尝试了所有方法,但 Recycleview 没有显示在屏幕上。我不断收到此错误“E/RecyclerView:未连接适配器;跳过布局'。这几天一直在断断续续地尝试解决这个问题。

顺便说一句,我正在使用 Fragment,这可能就是为什么这让我很痛苦。

uploadsFragment.java

private RecyclerView mRecylcerView;
private ImageAdapter mAdapter;
private ProgressBar mProgressCircle;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;

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

mRecylcerView =
(RecyclerView)view.findViewById(R.id.RecyclerView);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
mProgressCircle =
(ProgressBar)view.findViewById(R.id.progress_circle2);
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

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

mAdapter = new ImageAdapter(getActivity(), mUploads);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
mRecylcerView.setAdapter(mAdapter);
mProgressCircle.setVisibility(View.INVISIBLE);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(),databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
mProgressCircle.setVisibility(View.INVISIBLE);
}
});

return view;
}

ImageAdapter.java

private Context mContext;
private List<Upload> mUploads;

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

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

@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
Upload uploadCurrent = mUploads.get(i);
imageViewHolder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getmImageUrl())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(imageViewHolder.imageView);
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder{

public TextView textViewName;
public ImageView imageView;

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

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

最佳答案

在收到 Firebase 查询的响应后,您正在使用 RecyclerView 设置适配器。

  1. 因此,首先请确保您的 Firebase 查询返回成功的响应。
  2. 收到响应后检查“mUploads”列表是否不为空。

关于java - fragment -E/RecyclerView : No adapter attached; skipping layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56410680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com