当前位置: 首页 > 知识库问答 >
问题:

Android Studio中未显示回收器视图

蒲昊苍
2023-03-14

我正在制作一个应用程序,其中有回收器视图。但我无法在我的应用程序中看到回收器视图。当我检查Logcat时,它显示'e/recyclerview:No adapter attached;正在跳过布局‘。为什么会发生这种情况,我该如何修复?

我的主要活动代码:


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Adapter;
import android.widget.Toast;

import com.iammert.library.readablebottombar.ReadableBottomBar;

import java.util.ArrayList;
import java.util.List;

import Adapters.PostAdapter;
import Models.PostsModel;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    ReadableBottomBar readableBottomBar;
    LinearLayoutManager linearLayoutManager;
    List<PostsModel>postsList;
    PostAdapter post_adapter;

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



        setContentView(R.layout.activity_main);
        readableBottomBar = findViewById(R.id.readableBottomBar);
        readableBottomBar.setOnItemSelectListener(new ReadableBottomBar.ItemSelectListener() {
            @Override
            public void onItemSelected(int i) {
                switch (i){
                    case 0:
//                        Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
//                        Toast.makeText(MainActivity.this, "Search", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
//                        Toast.makeText(MainActivity.this, "Video", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
//                        Toast.makeText(MainActivity.this, "Favourite", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
//                        Toast.makeText(MainActivity.this, "Profile", Toast.LENGTH_SHORT).show();
                        break;


                }
            }
        });



    }

    private void post_RV_data() {
        postsList = new ArrayList<>();
        postsList.add(new PostsModel(R.drawable.story1,"Angle",R.drawable.ic_baseline_more_vert_24  ,R.drawable.story1,
                R.drawable.ic_baseline_favorite_border_24, "5.9K", R.drawable.ic_baseline_chat_bubble_24, "941",
                R.drawable.ic_baseline_send_24,"312", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story1,"Angle",R.drawable.ic_baseline_more_vert_24  ,R.drawable.story1,
                R.drawable.ic_baseline_favorite_border_24, "5.9K", R.drawable.ic_baseline_chat_bubble_24, "941",
                R.drawable.ic_baseline_send_24,"312", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story1,"Angle",R.drawable.ic_baseline_more_vert_24  ,R.drawable.story1,
                R.drawable.ic_baseline_favorite_border_24, "5.9K", R.drawable.ic_baseline_chat_bubble_24, "941",
                R.drawable.ic_baseline_send_24,"312", R.drawable.ic_baseline_bookmark_border_24));

    }

    private void post_RV_ids() {
        recyclerView = findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        post_adapter = new PostAdapter(postsList);
        recyclerView.setAdapter(post_adapter);
        post_adapter.notifyDataSetChanged();
    }
}
//This class is the data source
//This class acts as a structure for holding information of every item in the recycler view
package Models;

public class PostsModel {
    private int profile_pic;
    private String user_name;
    private int more;
    private int post_image;
    private int like;
    private String like_no;
    private int comment;
    private String comment_no;
    private int share;
    private String share_no;
    private int bookmark;

    public PostsModel(int profile_pic, String user_name, int more, int post_image, int like,
                      String like_no, int comment, String comment_no, int share, String share_no, int bookmark){
        this.profile_pic = profile_pic;
        this.user_name = user_name;
        this.more = more;
        this.post_image = post_image;
        this.like = like;
        this.like_no = like_no;
        this.share = share;
        this.share_no = share_no;
        this.comment = comment;
        this.comment_no = comment_no;
        this.bookmark = bookmark;
    }
    //Getters

    //Alt+Insert
    public int getProfile_pic() {
        return profile_pic;
    }

    public int getPost_image() {
        return post_image;
    }

    public int getMore() {
        return more;
    }

    public int getLike() {
        return like;
    }

    public int getComment() {
        return comment;
    }

    public int getShare() {
        return share;
    }

    public int getBookmark() {
        return bookmark;
    }

    public String getUser_name() {
        return user_name;
    }

    public String getLike_no() {
        return like_no;
    }

    public String getComment_no() {
        return comment_no;
    }

    public String getShare_no() {
        return share_no;
    }


}

我的适配器(PostAdapter.java)类代码:

//Adapter binds the data from datasource(PostModel) to recyclerview
package Adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.smart.instagram.R;

import java.util.List;

import Models.PostsModel;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

    private List<PostsModel> postList;

    public PostAdapter(List<PostsModel>postList){
        this.postList = postList;
    }

    @NonNull
    @Override
    //Implements design of our layout resource file
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts_rv, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int profile_pic = postList.get(position).getProfile_pic();
        int more = postList.get(position).getMore();
        int post_image = postList.get(position).getPost_image();
        int like = postList.get(position).getLike();
        int share = postList.get(position).getShare();
        int comment = postList.get(position).getComment();
        int bookmark = postList.get(position).getBookmark();
        String user_name = postList.get(position).getUser_name();
        String like_no = postList.get(position).getLike_no();
        String share_no = postList.get(position).getShare_no();
        String comment_no = postList.get(position).getComment_no();

        holder.setData(profile_pic, more, post_image, like, share, comment, bookmark,
                user_name, like_no, comment_no, share_no);

    }

    //Helps to bind data from MainActivity to post_rv


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


    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView profile_img, post_img, more_img, like_img, comment_img, share_img, bookmark_img;
        private TextView name, like_nos, comment_nos, share_nos;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            profile_img = itemView.findViewById(R.id.profile_pic);
            name = itemView.findViewById(R.id.user_name);
            more_img = itemView.findViewById(R.id.more);
            post_img = itemView.findViewById(R.id.post_image);
            like_img = itemView.findViewById(R.id.like);
            like_nos = itemView.findViewById(R.id.like_no);
            share_img = itemView.findViewById(R.id.share);
            share_nos = itemView.findViewById(R.id.share_no);
            comment_img = itemView.findViewById(R.id.comment);
            comment_nos = itemView.findViewById(R.id.comment_no);
            bookmark_img = itemView.findViewById(R.id.bookmark);

        }

        public void setData(int profile_pic, int more, int post_image, int like, int share, int comment, int bookmark,
                            String user_name, String like_no, String comment_no, String share_no) {
            profile_img.setImageResource(profile_pic);
            more_img.setImageResource(more);
            post_img.setImageResource(post_image);
            like_img.setImageResource(like);
            share_img.setImageResource(share);
            comment_img.setImageResource(comment);
            bookmark_img.setImageResource(bookmark);
            name.setText(user_name);
            like_nos.setText(like_no);
            comment_nos.setText(comment_no);
            share_nos.setText(share_no);
        }
    }


}

共有1个答案

桓修能
2023-03-14

在OnCreateViewWholder()中尝试.inflate(r.layout.posts_rv,false);也确保您的布局如下所示

<android.support.v7.widget.RecyclerView android:id="@+id/RecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" />
 类似资料:
  • 我正试图添加数据在我的回收视图从火炉。我有带名字的文件(级别级别编号)但没有显示我的活动这是代码: 主要活动水平: 公共类级别活动扩展AppCompat活动{ } 水平调整器 公共类级别适配器扩展了RecyclerView。适配器 }

  • 在我的应用程序中,我使用了一个回收器视图,它包含了许多项目,我想在项目之间显示一个点分隔线(分隔符),但它不起作用。我尝试创建一个可绘制的形状,但在添加DividerItemDecurity之后,在回收器视图项之间没有显示空间或线条。我也尝试过创建自定义DividerItemDecoration类,但对我来说都不起作用。注意:目前在我的可绘制形状被设置为矩形,我也尝试了线条。如何实现。任何帮助都将

  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我

  • 当我按下后退按钮时,这个ShoppingActivity会被破坏,当我再次访问该activity时,我发现我的回收器视图项目被替换了两次。我该如何解决这个问题? ShoppingActivity.java这里编写的所有代码和volley类也在这里调用,当我按下后退键并再次访问该活动时,我发现我的回收器视图项被替换了 @override protected void onCreate(Bundle

  • 当我尝试将Image URL解析到ImageView中时,回收器视图不显示,活动为空。我正在使用Picasso将图像加载到适配器类中的onBinfViewHolder方法中。这是相关代码 代表: } RepRvAdapter: } 解析JSON数据的方法: 现在,我有一行解析图像URL的代码被注释掉了。行取消注释后,活动将不再显示。如何获取要在ImageView中解析和显示的图像url?我认为这可

  • 这是我的活动 这是我的回收器适配器 logcat 这里,我从以前的活动回收器视图中获取值,并且我试图在此活动中通过Bundle显示它。 问题是,当我用这种方法执行操作时,活动没有显示任何东西,而我在我的Bundle中获取值,我没有获取它是什么问题。如有任何帮助,将不胜感激。谢谢你