来自服务器的Retreive数据onClick查看

时间:2017-03-30 17:43:13

标签: java android android-recyclerview retrofit

我有一个服务器,我从RecyclerView获取数据并显示,当我点击视图时,预计会打开另一个活动,显示特定视图的完整细节,因此ID该帖子用于获取完整内容。我通过意图将id传递给新的Activity,我收到一个错误: Caused by: java.lang.IndexOutOfBoundsException: Invalid index 34, size is 0 如何使用ID来进行Api调用以获取内容?

这是id = 34

项目的完整内容
{
  "blog_id": "34",
  "blog_title": "BLOG TITLE",
  "blog_subtitle": "Inspiration for Article Intro Effects",
  "blog_excerpt": "<p>Lorem ipsum dolor sit amet, veniam vidisse periculis ei mel, an nam malis nostrud euripidis. Ad invidunt explicari repudiandae qui, ut quo dolor sadipscing. Est laudem offendit ei. Id mentitum asse",
  "blog_content": "<p>We may define a food to be any substance which will repair the functional waste of the body, increase its growth, or maintain the heat, muscular, and nervous energy.</p>\r\n<p>In its most comprehensive sense, the oxygen of the air is a food; as although it is admitted by the lungs, it passes into the blood, and there re-acts upon the other food which has passed through the stomach. It is usual, however, to restrict the term food to such nutriment as enters the body by the intestinal canal. Water is often spoken of as being distinct from food, but for this there is no sufficient reason.</p>\r\n<p>Many popular writers have divided foods into flesh-formers, heat-givers, and bone-formers. Although attractive from its simplicity, this classification will not 
  "blog_thumbnail": "https://watchnollywood.ml/app/templates/default/",
  "blog_medimg": "https://watchnollywood.ml/app/templates/default/images/medium_57d4789461f8b.jpg",
  "category_title": "General"
}

BlogAdapter:

public class MyBlogAdapter extends RecyclerView.Adapter<BlogViewHolder> {
List<BlogPost> postsList;
List<SingleBlogPost> singleBlogPosts;
Context context;
private LayoutInflater inflater;
static  String blog_Id;
static String blogID;

public MyBlogAdapter(Context context, List<BlogPost> postsList){
    this.context = context;
    this.inflater = LayoutInflater.from(context);
    this.postsList = postsList;
}
@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.blog_post_item,parent, false);
    return new BlogViewHolder(view);
}

@Override
public void onBindViewHolder(BlogViewHolder holder, int position) {
    BlogPost posts= postsList.get(position);
    holder.summary.setText(posts.getBlogExcerpt().trim().toString());
    holder.title.setText(posts.getBlogTitle().trim().toString());
    Glide.with(context).load(posts.getBlogThumbnail()).into(holder.cover);

    holder.blogHolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, AnotherSingleView.class);
            blog_Id = posts.getBlogId();
            intent.putExtra(blogID,blog_Id);
            Log.d("MyblogAdapter","Please check blog Id: "+blog_Id);

            context.startActivity(intent);
        }
    });  
   }

@Override
public int getItemCount() {
    Log.d("MyBlogAdapter,","getItemCount"+postsList.size());

    return postsList == null ? (0) : postsList.size();
}
}

AnotherSingleView:

public class AnotherSingleView extends AppCompatActivity {
String postID;
int position = 0;
public TextView blogTitle,blogSub,blogContent;
public ImageView blogPic;
List<SingleBlogPost> singleBlogPosts;
 SingleBlogPost singleBlogPost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another_single_view);


    Intent intent  = getIntent();
    Bundle showBlogId = intent.getExtras();
    postID = showBlogId.getString(blogID);
    Log.d("AnotherSingleView","Please check blog Id: "+postID);

    singleBlogPosts = new ArrayList<>();

    blogContent = (TextView)findViewById(R.id.blog_content);
    blogSub = (TextView) findViewById(R.id.blog_subtitle);
    blogTitle =(TextView) findViewById(R.id.blog_title);
    blogPic =(ImageView) findViewById(R.id.blog_pix);
    singlePostDisplay();

}

private void singlePostDisplay() {
    singleBlogPost = singleBlogPosts.get(Interger.parseInt(postID));
    String post_id = singleBlogPost.getBlogId();
    if (postID.matches(post_id)){
        BlogaPI api = ApiClient.getBlogInterface();
      Call<List<SingleBlogPost>> call = api.postResponse();
        call.enqueue(new Callback<List<SingleBlogPost>>() {
            @Override
            public void onResponse(Call<List<SingleBlogPost>> call, Response<List<SingleBlogPost>> response) {
               singleBlogPosts = response.body();
                blogContent.setText(singleBlogPost.getBlogContent());
                blogSub.setText(singleBlogPost.getBlogSubtitle());
                blogTitle.setText(singleBlogPost.getBlogTitle());
                Glide.with(AnotherSingleView.this).load(singleBlogPost.getBlogMedimg()).into(blogPic);
            }

            @Override
            public void onFailure(Call<List<SingleBlogPost>> call, Throwable t) {

            }
        });

    }

}
}

接口:

public interface BlogRequestInterface {

  @GET("blog")
  Call<BlogApi> getMyBlog();
  @GET("post/{id}")
  Call<List<SingleBlogPost>> getSinglePost();
}

Pojo课程:

public class SingleBlogPost {

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

1 个答案:

答案 0 :(得分:2)

我不建议在onBindViewHolder上添加ClickListeners。我的onCreateViewHolder就是这样的。

   @Override
   public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.blog_post_item,parent, false);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do stuff
        }
    });

    return new BlogViewHolder(view);
}

如果您需要位置或列表对象,可以参考此链接http://androidshenanigans.blogspot.com.tr/2015/02/viewholder-pattern-common-mistakes.html

希望它有所帮助。