问题描述
我使用 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) {
}
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)