skip to Main Content

I’m trying to set up a Recyclerview with multiple lists(like Facebook ads and user feed) from 2 API endpoints but getting some errors when I try to paginate the list(load more).

Here is my code. I’ve tried to call the addAll method in my activity after loading the two lists from the different API endpoints but I get an error(I also call this method in my loadMore listener[paginate stuff]).

private static final int TYPE_VIDEO = 0;
private static final int TYPE_PROMOTED_VIDEO = 1;
private static final int TYPE_LOADING = 3;

private Context context;
List<Video> videosList;
List<Promoted> promotedVideosList;

private boolean isLoadingAdded = true;  

public MyAdapter(Context context){
    this.context = context;
    //Initalization stuffs
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case TYPE_VIDEO:
            View viewVideo = inflater.inflate(R.layout.item_video, parent, false);
            viewHolder = new VideoVH(viewVideo);
            break;

        case TYPE_PROMOTED_VIDEO:
            View viewPromoted = inflater.inflate(R.layout.item_promoted_video, parent, false);
            viewHolder = new PromotedVH(viewPromoted);
            break;

        case TYPE_LOADING:
            View viewLoading = inflater.inflate(R.layout.item_loading, parent, false);
            viewHolder = new LoadingVH(viewLoading);
            break;

    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    switch (getItemViewType(position)) {

        case TYPE_VIDEO:
            //DO something when it a normal video
            break;

        case TYPE_PROMOTED_VIDEO:
            //DO something when it's a promoted video

            break;

        case TYPE_LOADING:
            //Loading view
            break;
    }

}


public void add(Video video, Promoted promoted) {
    Toast.makeText(context, "Not Null", Toast.LENGTH_SHORT).show();
    videosList.add(video);
    promotedVideosList.add(promoted);

    notifyItemInserted(videosList.size() + promotedVideosList.size() - 1);
}

public void addAll(List<Video> videoList, List<Promoted> promotedList) {
    add(addVideo(videoList), addPromoted(promotedList));
}

private Video addVideo(List<Video> videoList){
    for (Video result : videoList) return result;
    return null;
}

private Promoted addPromoted(List<Promoted> promotedList{
    for (Promoted result : promotedList) return result;
    return null;
}


@Override
public int getItemCount() {
    return videosList == null && promotedVideosList == null ? 0 : this.videosList.size() + this.promotedVideosList.size();
}

@Override
public int getItemViewType(int position) {
    int videoSize = this.videosList.size();
    int promotedSize = this.promotedVideosList.size();

    if(position < videoSize){
        return TYPE_VIDEO;
    }

    if(position - videoSize < promotedSize){
        return TYPE_PROMOTED_VIDEO;
    }

    if (position == promotedSize + videoSize - 1 && isLoadingAdded){
        return TYPE_LOADING;
    }

    return -1;
}

public static class VideoVH extends RecyclerView.ViewHolder{

    VideoVH(View itemView) {
        super(itemView);

        //findviewbyid stuff

    }
}

public static class PromotedVH extends RecyclerView.ViewHolder{

    public PromotedVH(View itemView) {
        super(itemView);
      //findviewbyid stuff
    }
}

public static class LoadingVH extends RecyclerView.ViewHolder{

    @BindView(R.id.loading_progress)
    ProgressBar loading;

    public LoadingVH(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

I expected this to work but somehow it doesn’t and I know I’m not doing something right.

Here is a snippet from my logcat.

 java.lang.NullPointerException: Attempt to invoke interface method'boolean java.util.List.add(java.lang.Object)' on a null object reference
 at com.myproject.adapters.MyAdapter.add(MyAdapter.java:508)
 at com.myproject.adapters.MyAdapter.addAll(MyAdapter.java:515)

2

Answers


  1. Have you initialize the lists before add data into it.In your constructor you must initialize all lists.
    For Example : videosList = new List();
    likewise

    Login or Signup to reply.
  2. You are not initializing videosList and promotedVideosList . Do this

    public MyAdapter(Context context){
      //Initalization stuffs
       this.context = context;
       videosList=new ArrayList();
       promotedVideosList=new ArrayList();
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search