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

时间:2017-03-29 15:03:46

标签: java android retrofit

在应用程序中我从服务器获取数据,但是当我运行程序时,我没有收到任何错误,我在屏幕上看不到任何内容。基本上我的RecyclerView是空白的。我不知道出了什么问题。 MainActivity类:

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) {

    }
}

适配器类:

public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.RetrofitViewHolder> {
List<BlogResponse> blogResponseList;
Context context;


public RetrofitAdapter (List<BlogResponse> blogResponseList, Context context){
    this.blogResponseList = blogResponseList;
    this.context = context;
}
@Override
public RetrofitAdapter.RetrofitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_view, parent, false);
    return new RetrofitViewHolder(view);
}

@Override
public void onBindViewHolder(RetrofitViewHolder holder, int position) {
    Log.i("RetrofitAdapter", "onBindViewHolder: "+ position);
    BlogResponse blogResponse = blogResponseList.get(position);
    holder.summary.setText(blogResponse.getBlogExcerpt());
    holder.title.setText(blogResponse.getBlogTitle());
    holder.thumbnail_cover.setImageResource(Integer.getInteger(blogResponse.getBlogThumbnail()));
}

@Override
public int getItemCount() {
    return blogResponseList.size() ;
}
public void updateItems(List items ){
    this.blogResponseList = items;
    notifyDataSetChanged();
}



public class RetrofitViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView title;
    TextView summary;
    ImageView thumbnail_cover;
    public RetrofitViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.post_title);
        summary = (TextView) itemView.findViewById(R.id.post_summary);
        thumbnail_cover = (ImageView) itemView.findViewById(R.id.post_thumbnail);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        BlogResponse items = getItem(getAdapterPosition());
        this.getAdapterPosition();
        notifyDataSetChanged();
    }
}

private BlogResponse getItem(int adapterPosition) {
    return blogResponseList.get(adapterPosition);
}
}

Blogapi界面

 public interface BlogaPI {
  @GET("blog")
  Call<BlogList> getBlogpost();
}

BlogList类:

public class BlogList  {
    @SerializedName("blog")
    @Expose
    private ArrayList<BlogResponse> blogResponses = new ArrayList<>();
}

BlogResponse类

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;
    }
}

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;
}
}

请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

使用RecyclerView.Adapter<>时需要注意的基本事项,你遇到上述问题

  

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

  1. 确保您的数据正确传递
    尝试将数据集大小打印到getItemCount()方法

  2. 确保row_item身高设置为wrap_content(初学者常犯的错误。)

  3. 确保使用datasetadd()将项目添加到addAll(),而不是直接分配参考。