我使用 json api Retrofit 和 Wordpress 作为后端制作应用程序,前 10 个帖子运行顺畅,当我点击这些帖子时,我可以看到帖子详细信息,但是当我滚动以获取更多帖子时,我可以看到帖子但是当我点击我看到这个错误:
Caused by: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
at java.util.ArrayList.get(ArrayList.java:437)
at com.punjabidharti.myapplication.PostDetails.onCreate(PostDetails.java:30)
和我的 PostDetails.Class
Intent i = getIntent();
int position = i.getExtras().getInt("itemPosition");
Log.e("PostDetails ", "title is " + MainActivity.mListPost.get(position).getTitle().getRendered());
this.title = (TextView) findViewById(R.id.title);
title.setText(Html.fromHtml(MainActivity.mListPost.get(position).getTitle().getRendered()));
String data = String.valueOf((Html.fromHtml(MainActivity.mListPost.get(position).getContent().getRendered())));
WebView webview = (WebView)this.findViewById(R.id.postwebview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadData(data, "text/html; charset=utf-8", "UTF-8");
这就是我在滚动上获得更多帖子的方式:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
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&page=2";
getRetrofit();
loading = true;
}
}
}
}
});
这就是我从 Retrofit 获取数据的方式:
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("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).getLinks().getWpAttachment().get(0).getHref()));
}
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.GONE);
}
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<WPPost>> call, Throwable t) {
}
});
我已经搜索了一个多星期,但仍然找不到答案。请帮忙