Unable to get the Cardview, E/RecyclerView: No adapter attached; skipping layout error , how to add adapter to recyclerView?

huangapple go评论107阅读模式
英文:

Unable to get the Cardview, E/RecyclerView: No adapter attached; skipping layout error , how to add adapter to recyclerView?

问题

以下是你提供的代码的中文翻译:

我正在使用Android Studio构建一个应用程序在运行应用程序时我遇到了E/RecyclerView: No adapter attached; skipping layout的问题
由于我对Android Studio开发还很新无法找出问题所在我尝试了一些在线解决方案但对我没有起作用

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerViewAdapter adapter;
    DatabaseReference mDatabase;
    ProgressDialog progressDialog;
    private List<Upload> uploads;

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

        recyclerView = findViewById(R.id.recyclerview_id);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        progressDialog = new ProgressDialog(this);
        uploads = new ArrayList<>();
        progressDialog.setMessage("请稍候...");
        progressDialog.show();
        mDatabase = FirebaseDatabase.getInstance().getReference("upload");
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                progressDialog.dismiss();
                for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
                    Upload upload = postsnapshot.getValue(Upload.class);
                    uploads.add(upload);
                }
                adapter = new RecyclerViewAdapter(getApplicationContext(), uploads);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                progressDialog.dismiss();
            }
        });
    }
}

**适配器**
这是我的适配器代码有时候会出错或者我可能犯了一个错误请检查这两个地方并给我一个纠正这个问题的想法

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private List<Upload> uploads;

    public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
        this.mContext = mContext;
        this.uploads = uploads;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final Upload upload = uploads.get(position);
        holder.tv_book_title.setText(upload.getName());
        Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_book_title;
        ImageView imd_book_thumbnail;
        CardView cardView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_book_title = itemView.findViewById(R.id.book_title_id);
            imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
            cardView = itemView.findViewById(R.id.card_view_id);
        }
    }
}
英文:

I'm using the android studio to build an application. I'm getting "E/RecyclerView: No adapter attached; skipping layout" when running the app.
Since I'm new to android studio development, unable to figure out the problem. I tried several solutions online, but it didn't work for me.

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
DatabaseReference mDatabase;
ProgressDialog progressDialog;
private List &lt;Upload&gt; uploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview_id);
recyclerView.setLayoutManager(new GridLayoutManager(this,3));
progressDialog = new ProgressDialog(this);
uploads = new ArrayList&lt;&gt;();
progressDialog.setMessage(&quot;Please wait ...&quot;);
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference(&quot;upload&quot;);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
progressDialog.dismiss();
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Upload upload = postsnapshot.getValue(Upload.class);
uploads.add(upload);
}
adapter = new RecyclerViewAdapter(getApplicationContext(),uploads);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}

Adapter
This is my adapter code, sometime it will be wrong or I had a mistake check these two and please give me an idea to correct this problem.

public class RecyclerViewAdapter extends  RecyclerView.Adapter&lt;RecyclerViewAdapter.MyViewHolder&gt;{
private Context mContext;
private List &lt;Upload&gt; uploads;
public RecyclerViewAdapter(Context mContext, List&lt;Upload&gt; uploads) {
this.mContext = mContext;
this.uploads = uploads;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.card_view_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final  Upload upload = uploads.get(position);
holder.tv_book_title.setText(upload.getName());
Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}
@Override
public int getItemCount() {
return uploads.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView imd_book_thumbnail;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_book_title = itemView.findViewById(R.id.book_title_id);
imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
cardView = itemView.findViewById(R.id.card_view_id);
}
}

}

答案1

得分: 1

你需要在 onCreate 方法中设置适配器,目前你是在监听器中设置的。我猜想你只需要将 recylerView.setAdapter(adapter) 移动到 onCreate 中,然后在你的监听器中设置数据和调用 notifyDataSetChanged

应该是这样的:

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

    recyclerView = findViewById(R.id.recyclerview_id);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
    adapter = new RecyclerViewAdapter(getApplicationContext());
    recyclerView.setAdapter(adapter);
    progressDialog = new ProgressDialog(this);
    uploads = new ArrayList<>();
    progressDialog.setMessage("请稍候...");
    progressDialog.show();
    mDatabase = FirebaseDatabase.getInstance().getReference("upload");
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            progressDialog.dismiss();
            for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
                Upload upload = postsnapshot.getValue(Upload.class);
                uploads.add(upload);
            }
            adapter.setList(uploads);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
}

适配器部分

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private List<Upload> uploads;

    public RecyclerViewAdapter(Context mContext, List<Upload> uploads) {
        this.mContext = mContext;
        this.uploads = uploads;
    }

    public RecyclerViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<Upload> uploads) {
        this.uploads = uploads;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final Upload upload = uploads.get(position);
        holder.tv_book_title.setText(upload.getName());
        Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
    }

    @Override
    public int getItemCount() {
        return uploads == null ? 0 : uploads.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_book_title;
        ImageView imd_book_thumbnail;
        CardView cardView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_book_title = itemView.findViewById(R.id.book_title_id);
            imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
            cardView = itemView.findViewById(R.id.card_view_id);
        }
    }
}
英文:

You have to set the adapter in onCreate method, right now you're doing it in a listener. My guess is you just need to move the recylerView.setAdapter(adapter) in onCreate and just set the datas and notifyDataSetChanged in your listener

It should look like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recyclerview_id);
    recyclerView.setLayoutManager(new GridLayoutManager(this,3));
    adapter = new RecyclerViewAdapter(getApplicationContext());
    recyclerView.setAdapter(adapter)
    progressDialog = new ProgressDialog(this);
    uploads = new ArrayList&lt;&gt;();
    progressDialog.setMessage(&quot;Please wait ...&quot;);
    progressDialog.show();
    mDatabase = FirebaseDatabase.getInstance().getReference(&quot;upload&quot;);
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            progressDialog.dismiss();
            for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
                Upload upload = postsnapshot.getValue(Upload.class);
                uploads.add(upload);
            }
            adapter.setList(uploads);
        }

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

            progressDialog.dismiss();

        }
    });

}

Adapter

public class RecyclerViewAdapter extends  RecyclerView.Adapter&lt;RecyclerViewAdapter.MyViewHolder&gt;{
private Context mContext;
private List&lt;Upload&gt; uploads;
public RecyclerViewAdapter(Context mContext, List&lt;Upload&gt; uploads) {
this.mContext = mContext;
this.uploads = uploads;
}
public RecyclerViewAdapter(Context mContext) {
this.mContext = mContext;
}
public void setList(List&lt;Upload&gt; uploads) {
this.uploads = uploads
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.card_view_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final  Upload upload = uploads.get(position);
holder.tv_book_title.setText(upload.getName());
Glide.with(mContext).load(upload.getUrl()).into(holder.imd_book_thumbnail);
}
@Override
public int getItemCount() {
return uploads == null ? 0 : uploads.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_book_title;
ImageView imd_book_thumbnail;
CardView cardView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_book_title = itemView.findViewById(R.id.book_title_id);
imd_book_thumbnail = itemView.findViewById(R.id.book_img_id);
cardView = itemView.findViewById(R.id.card_view_id);
}
}

huangapple
  • 本文由 发表于 2020年5月3日 17:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61572159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定