Java 我的视图在我的RecyclerView中不可见

Java 我的视图在我的RecyclerView中不可见,java,android,retrofit,Java,Android,Retrofit,在am应用程序中,我从服务器获取数据,但当我运行程序时,我没有收到任何错误,也没有在屏幕上看到任何内容。所以我的回收视图基本上是空白的。我不知道怎么了。 主要活动类别: public class MainActivity extends AppCompatActivity implements RecyclerView.OnClickListener { RecyclerView recyclerView; List<BlogResponse> blogRespons

在am应用程序中,我从服务器获取数据,但当我运行程序时,我没有收到任何错误,也没有在屏幕上看到任何内容。所以我的回收视图基本上是空白的。我不知道怎么了。 主要活动类别:

public class MainActivity extends AppCompatActivity implements RecyclerView.OnClickListener {
    RecyclerView recyclerView;
    List<BlogResponse> blogResponses;
    RetrofitAdapter retrofitAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        blogResponses = new ArrayList<>();
        recyclerView =(RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager linear = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linear);
        getBlogpost();

        recyclerView.setOnClickListener(this);
    }

    private void getBlogpost() {
        //final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data for blog", " Please wait...", false, false);
        BlogaPI apiService = ApiClient.getClient().create(BlogaPI.class);
        Call<BlogList> call = apiService.getBlogpost();
        call.enqueue(new Callback<BlogList>() {
            @Override
            public void onResponse(Call<BlogList> call, Response<BlogList> response) {
               // loading.dismiss();
                if (response.isSuccessful()){
                    blogResponses = response.body().getBlogResponses();
                    retrofitAdapter = new RetrofitAdapter(blogResponses,getApplicationContext());
                    recyclerView.setAdapter(retrofitAdapter);
                } else {
                    Toast.makeText(MainActivity.this, "Something is wrong", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<BlogList> call, Throwable t) {
            //    loading.dismiss();
                Log.e("MainActivity:", t.toString());

            }
        });/*
        Call<List<BlogResponse>> call = apiService.getBlogpost();
        call.enqueue(new Callback<List<BlogResponse>>() {
            @Override
            public void onResponse(Call<List<BlogResponse>> call, Response<List<BlogResponse>> response) {
                List<BlogResponse> blogresp = response.body();
                recyclerView.setAdapter(new RetrofitAdapter(blogresp,R.layout.custom_view,getApplicationContext(),
                        new RetrofitAdapter.ItemListener(){

                            @Override
                            public void onPostClick(String blogId) {
                                Toast.makeText(getApplicationContext(), "Post ID is " + blogId, Toast.LENGTH_SHORT).show();
                            }
                        }));
            }

            @Override
            public void onFailure(Call<List<BlogResponse>> call, Throwable t) {
                Log.e("MainActivity:", t.toString());
            }
        });*/
    }

    @Override
    public void onClick(View view) {

    }
}
ApiClient:

 public class ApiClient {
    private static final String STAGING_BASE_URL = "https://watchnollywood.ml/api/";
    private  static Retrofit retrofit = null;

    public static Retrofit getClient(){
      if (retrofit == null){
        retrofit = new Retrofit.Builder()
               .baseUrl(STAGING_BASE_URL)
               .addConverterFactory(GsonConverterFactory.create())
               .build();
   }
    return retrofit;
}
}

请帮忙。谢谢

在使用
RecyclerView.Adapter
时需要注意的基本问题,您遇到了上述问题

我的视图在我的RecyclerView中不可见

  • 确保数据正确传递
    尝试将数据集大小打印到
    getItemCount()
    方法中

  • 确保
    row\u项目
    height设置为
    wrap\u content
    (初学者常犯的错误)

  • 确保使用
    add()
    addAll()
    将项目添加到
    数据集中,而不是直接分配引用

  • 能否验证传递给适配器的列表中是否包含数据?这会让您知道这是JSON解析问题还是适配器问题。您是否从这个
    Log.i(“RefughtAdapter”、“onBindViewHolder:+position”)中得到任何打印信息?当我记录getItemCount()时,请上载回收器和活动的布局,结果为=0。我没有看到什么可以使它不能从服务器上拉。请提供任何其他建议不知何故,从服务器返回的
    JSON
    未按需要进行解析。再检查一遍。
     public interface BlogaPI {
      @GET("blog")
      Call<BlogList> getBlogpost();
    }
    
    public class BlogList  {
        @SerializedName("blog")
        @Expose
        private ArrayList<BlogResponse> blogResponses = new ArrayList<>();
    }
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class BlogResponse {
    
        @SerializedName("blog_id")
        @Expose
        private String blogId;
        @SerializedName("blog_title")
        @Expose
        private String blogTitle;
        @SerializedName("blog_subtitle")
        @Expose
        private String blogSubtitle;
        @SerializedName("blog_excerpt")
        @Expose
        private String blogExcerpt;
        @SerializedName("blog_content")
        @Expose
        private String blogContent;
        @SerializedName("blog_thumbnail")
        @Expose
        private String blogThumbnail;
        @SerializedName("blog_medimg")
        @Expose
        private String blogMedimg;
        @SerializedName("category_title")
        @Expose
        private String categoryTitle;
        public  BlogResponse(String blogId, String blogTitle, String blogSubtitle, String blogExcerpt,String blogContent, String blogThumbnail, String blogMedimg){
            this.blogId = blogId;
            this.blogTitle = blogTitle;
            this.blogSubtitle = blogSubtitle;
            this.blogExcerpt = blogExcerpt;
            this.blogContent = blogContent;
            this.blogThumbnail = blogThumbnail;
            this.blogMedimg = blogMedimg;
        }
    
        public String getBlogId() {
            return blogId;
        }
    
        public void setBlogId(String blogId) {
            this.blogId = blogId;
        }
    
        public String getBlogTitle() {
            return blogTitle;
        }
    
        public void setBlogTitle(String blogTitle) {
            this.blogTitle = blogTitle;
        }
    
        public String getBlogSubtitle() {
            return blogSubtitle;
        }
    
        public void setBlogSubtitle(String blogSubtitle) {
            this.blogSubtitle = blogSubtitle;
        }
    
        public String getBlogExcerpt() {
            return blogExcerpt;
        }
    
        public void setBlogExcerpt(String blogExcerpt) {
            this.blogExcerpt = blogExcerpt;
        }
    
        public String getBlogContent() {
            return blogContent;
        }
    
        public void setBlogContent(String blogContent) {
            this.blogContent = blogContent;
        }
    
        public String getBlogThumbnail() {
            return blogThumbnail;
        }
    
        public void setBlogThumbnail(String blogThumbnail) {
            this.blogThumbnail = blogThumbnail;
        }
    
        public String getBlogMedimg() {
            return blogMedimg;
        }
    
        public void setBlogMedimg(String blogMedimg) {
            this.blogMedimg = blogMedimg;
        }
    
        public String getCategoryTitle() {
            return categoryTitle;
        }
    
        public void setCategoryTitle(String categoryTitle) {
            this.categoryTitle = categoryTitle;
        }
    }
    
     public class ApiClient {
        private static final String STAGING_BASE_URL = "https://watchnollywood.ml/api/";
        private  static Retrofit retrofit = null;
    
        public static Retrofit getClient(){
          if (retrofit == null){
            retrofit = new Retrofit.Builder()
                   .baseUrl(STAGING_BASE_URL)
                   .addConverterFactory(GsonConverterFactory.create())
                   .build();
       }
        return retrofit;
    }
    }