【发布时间】:2021-03-30 03:07:42
【问题描述】:
我是菜鸟,想用 json api 和 WordPress 作为后端制作应用程序。我可以获取帖子并且它工作正常,直到第一页帖子,当我点击每个帖子时,我也可以看到帖子详细信息,但是在下一页帖子或滚动到下一页时,点击不起作用并且应用程序崩溃,但我也可以在下一页看到帖子图片,但看不到帖子详细信息。我搜索了很多,但似乎没有什么对我有用。我得到的错误是这样的:
java.lang.IndexOutOfBoundsException: Index: 11, Size: 10
当我点击第 12 个帖子时,我看到了这个
java.lang.IndexOutOfBoundsException: Index: 12, Size: 10
我不知道我怎样才能使尺寸超过 10..
我在谷歌上搜索了很多但没有得到任何解决方案..
这是我加载更多帖子的代码
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) { //check for scroll down
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = false;
Log.v("...", "Last Item Wow !");
// Do pagination.. i.e. fetch new data
yourURL = "https://punjabidharti.com/wp-json/wp/v2/posts/categories=4514&per_page=10" + "&page=" + pageNo++;
getRetrofit();
recyclerView.setAdapter(adapter);
和GetRetrofit
public void getRetrofit(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
String yourURl = yourURL.replace(baseURL,"");
Call<List<WPPost>> call = service.getPostInfo( yourURl);
call.enqueue(new Callback<List<WPPost>>() {
@Override
public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) {
Log.e("mainactivyt", " response "+ response.body());
mListPost = response.body();
progressBar.setVisibility(View.GONE);
if (response.body() != null) {
for (int i = 0; i < response.body().size(); i++ ) {
Log.e("size", list.size() + "");
System.out.println("The shortest word i is:" + i );
Log.e("main ", " title " + response.body().get(i).getTitle().getRendered() + " " +
response.body().get(i).getId());
String tempdetails = response.body().get(i).getExcerpt().getRendered().toString();
tempdetails = tempdetails.replace("<p>", "");
tempdetails = tempdetails.replace("</p>", "");
tempdetails = tempdetails.replace("[…]", "");
list.add(new Model(Model.IMAGE_TYPE, response.body().get(i).getTitle().getRendered(),
tempdetails,
response.body().get(i).getImages().getMedium()));
}
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.GONE);
}
adapter.notifyDataSetChanged();
【问题讨论】:
标签: android scroll retrofit adapter indexoutofboundsexception