0

i am making app with Android Studio using Rest Api from my WordPress website , i have done all and its working , now i have implemented adMob ads between post and it also works but the problem is when ad shows then the very last post is missing from list, like i have 6 posts in my website and when ad shows once in between posts than the 6th post is missing. this is my code in adapter..

@Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, int position) {
        AuthorModel authormodel = dataset.get(getRealPosition(position));

        if (holder instanceof MyDataHolder){
            ((MyDataHolder)holder).showModel(authormodel);
        }

    }

    private int getRealPosition(int position) {
        if (LIST_AD_DELTA == 0) {
            return position;
        } else {
            return position - position / LIST_AD_DELTA;
        }
    }

    @Override
    public int getItemCount() {
        Log.d("SIZE", String.valueOf(dataset.size()));
        return dataset.size();
    }
    @Override
    public int getItemViewType(int position){
        if (position > 0 && position % LIST_AD_DELTA == 0) {
            return AD;
        }

        return viewTypeData;
    }

you can see i log the dataset.size and it returns 6 that should be 7 i think when include ads ,

when is use code like this than all posts are showing but no ads,,

private int getRealPosition(int position) {
       
            return position;
        
    }

    @Override
    public int getItemCount() {
        Log.d("SIZE", String.valueOf(dataset.size()));
        return dataset.size();
    }
    @Override
    public int getItemViewType(int position){

        return viewTypeData;
    }

but i also want to insert ads without losing post, please help

noorapps
  • 39
  • 1
  • 6

1 Answers1

0

Use Live Data to get API information. Also, create a function for editing and delete old data the inside of the function and write the new data. Thus, every time you get an ad from the API, the data size changes and the item increases or decreases