E/RecyclerView:没有附加适配器;在片段内的recyclerview上跳过布局

Posted

技术标签:

【中文标题】E/RecyclerView:没有附加适配器;在片段内的recyclerview上跳过布局【英文标题】:E/RecyclerView: No adapter attached; skipping layout on recyclerview inside fragment 【发布时间】:2020-07-09 17:59:11 【问题描述】:

我收到“E/RecyclerView:未连接适配器;跳过布局”错误消息。 我在 Fragment 中有 recycleView,所以我一定在 fragment1.java 上做错了。

这里是代码。如您所见,我在代码上设置了适配器,但出现此错误。任何帮助,将不胜感激。谢谢。

公共类 Fragment1 扩展 Fragment

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Bible> arrayList;
private FirebaseDatabase database;
private DatabaseReference databaseReference;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);

    recyclerView = rootView.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    //GridLayoutManager layoutManager = new GridLayoutManager(getContext(),  2);
    layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);


    arrayList = new ArrayList<>();

    database = FirebaseDatabase.getInstance();
    databaseReference = database.getReference("Bible");

    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() 
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
            arrayList.clear();
            for(DataSnapshot snapshot: dataSnapshot.getChildren())
                Bible bible = snapshot.getValue(Bible.class);
                arrayList.add(bible);

            
            adapter.notifyDataSetChanged();
        

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) 

            Log.e("fragment1", String.valueOf(databaseError.toException()));//에러문 출력
        
    );

    adapter = new BibleAdapter(arrayList, getContext());
    recyclerView.setAdapter(adapter);






    return rootView;

公共类 BibleAdapter 扩展 RecyclerView.Adapter

private  ArrayList<Bible> arrayList;
private  Context context;
private OnVerseItemClickListener listener;

public BibleAdapter(ArrayList<Bible> arrayList, Context context) 
    this.arrayList = arrayList;
    this.context = context;


public void setOnItemClickListener(OnVerseItemClickListener listener)
    this.listener = listener;


@NonNull
@Override
public BibleAdapter.BibleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.verse_item, parent, false);
    BibleViewHolder holder = new BibleViewHolder(view, listener);
    return holder;


@Override
public void onBindViewHolder(@NonNull BibleAdapter.BibleViewHolder holder, int position) 
    Glide.with(holder.imageView)
            .load(arrayList.get(position).getImage())
            .into(holder.imageView);
    holder.bibleType.setText(arrayList.get(position).getBibleType());
    holder.keysentance.setText(arrayList.get(position).getKeysentance());
    holder.verse.setText(arrayList.get(position).getVerse());



@Override
public int getItemCount() 
    return (arrayList!=null? arrayList.size():0);


public class BibleViewHolder extends RecyclerView.ViewHolder

    ImageView imageView;
    TextView bibleType;
    TextView verse;
    TextView keysentance;

    public BibleViewHolder(@NonNull View itemView, final OnVerseItemClickListener listener) 
        super(itemView);
        this.imageView = itemView.findViewById(R.id.imageView);
        this.bibleType = itemView.findViewById(R.id.bibleType);
        this.keysentance = itemView.findViewById(R.id.keysentance);
        this.verse = itemView.findViewById(R.id.verse);
        itemView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                int position = getAdapterPosition();
                if(listener != null)
                    listener.onItemClick(BibleViewHolder.this, v, position);
                
            
        );
    

【问题讨论】:

【参考方案1】:

在您的代码中,您创建了带有空 arrayList 的适配器,但之后您永远不会更新列表。

在适配器中创建 swapData() 方法并在您必须刷新数据或已刷新数据时调用它,在您的情况下,您将在 onDataChange() 中获取它。

并且还要在添加监听器之前定义适配器:

adapter = new BibleAdapter(arrayList, getContext());
        recyclerView.setAdapter(adapter);

databaseReference.addListenerForSingleValueEvent(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                arrayList.clear();
                for(DataSnapshot snapshot: dataSnapshot.getChildren())
                    Bible bible = snapshot.getValue(Bible.class);
                    arrayList.add(bible);
                
                  adapter.swapData(arrayList);
                //adapter.notifyDataSetChanged();
            

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) 

                Log.e("fragment1", String.valueOf(databaseError.toException()));//에러문 출력
            
        );

在适配器类中创建方法为:

public void swapData(ArrayList<Bible> filtersList) 
    if (filtersList != null) 
        this.arrayList.clear();
        this.arrayList.addAll(filtersList);
        notifyDataSetChanged();
    

【讨论】:

我将 swapdata 放入适配器并将 RecyclerView.Adapter 适配器更改为 BibleAdapeter 适配器。但仍然无法正常工作。 有没有调试过arrayList是否为空?/ 我在代码上将 RecyclerView.Adapter 适配器更改为 BibleAdapter 适配器。现在它部分工作。当我将图像存储到数据库中时,图像不显示。我使用 gs 链接而不是 https 链接,因为 https 链接不再存在。你知道怎么把图片存入数据库吗?

以上是关于E/RecyclerView:没有附加适配器;在片段内的recyclerview上跳过布局的主要内容,如果未能解决你的问题,请参考以下文章

Fragment,RecyclerView:没有附加适配器;跳过布局

recyclerView 没有适配器附加,跳过布局

Firebase:Recyclerview 没有附加适配器,跳过布局

Firebase:Recyclerview 没有附加适配器,跳过布局

Recyclerview 未连接适配器

尝试设置适配器:不断获取 E/RecyclerView:未连接适配器;跳过布局[重复]