0

i make app with json api Retrofit and Wordpress as backend, first 10 posts works smooth, when i click on those posts i can see the post details, but when i scroll to get more posts, i can see the posts but when i click on it i see this error:

 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)

and my 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");

and this is how i get more posts on scroll :

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

this is how i get data from 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("[&hellip;]", "");
                    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) {
        }
    });

i have searched a lot for more than week but still unable to find answers. please help

  • Based on the error it seems like you don't enough items to show from the arraylist and you are trying to access invalid item from invalid index. Can you post code for getRetrofit() function ? because it seem that you might be trying to load recyclerview before you get the data from retrofit call. – deeppandya Mar 26 '21 at 05:39
  • please check my update question @deeppandya – Punjabi Dharti Mar 26 '21 at 06:04

0 Answers0