1

I'm developing a e-commerce application for a start-up, to display the products enlisted in respective categories i have used Recycler view. For single variant products the template which i customized is working perfectly it contains a imageview, couple of textboxes and a spinner list. The problem emerged when i tried to create a viewholder for multiple variant product with separate model class and integrated to the recycler using the multiple view type conjecture i have cleared the initial error "Index out of bound Size 1 Index 1" but a resources Oxo (Null pointer Exception) error is still unclear and i am blank at this instance, any help will be appreciated.

have already surfed tons, stackoverflow and other repositories for reference such as multiple viewtype, implementing multipleviews etc..

Trial Code 1

public class BreadAdapter extendsRecyclerView.Adapter<BreadAdapter.Viewholder> {


private ImageView imageView1;

private TextView title1;

private TextView price1;

private TextView subtitle1;

private Spinner qty_spinner1;

private List<BreadModelClass> breadModelClassList;

private List<BreadModelClass2> breadModelClass2List;

private Context context;

public BreadAdapter(List<BreadModelClass> breadModelClassList,List<BreadModelClass2> breadModelClass2List) {

    this.breadModelClassList = breadModelClassList;

    this.breadModelClass2List = breadModelClass2List;


}

@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    context = parent.getContext();

    View view = inflater.inflate(viewType,parent,false);

    Viewholder viewholder = null;


    switch (viewType){

        case R.layout.bread_item_layout:

            viewholder = new Viewholder(view);

            break;

        case R.layout.bread_multipleview:

            viewholder = new Viewholder1(view);

            break;

    }

    return viewholder;

}





@Override
public void onBindViewHolder(@NonNull final Viewholder viewholder, final int position) {


    switch (viewholder.getItemViewType()){

        case 0:

            String resource = breadModelClassList.get(position).getImage_resource();

            String title = breadModelClassList.get(position).getTitle();

            int price = breadModelClassList.get(position).getPrice();

            String desc = breadModelClassList.get(position).getDesc();

            String subtitle = breadModelClassList.get(position).getSubtitle();

            viewholder.setData(resource,title,price,desc,subtitle);

            break;

        case 1:

            String resource1 = breadModelClass2List.get(position).getImage_resource1();

            String title1 = breadModelClass2List.get(position).getTitle1();

            int price1 = breadModelClass2List.get(position).getPrice1();

            String desc1 = breadModelClass2List.get(position).getDesc1();

            String desc2 = breadModelClass2List.get(position).getDesc2();

            String subtitle1 = breadModelClass2List.get(position).getSubtitle1();

            viewholder.setData1(resource1,title1,price1,desc1,desc2,subtitle1);

            break;

    }

    viewholder.getAdapterPosition();

}

@Override
public int getItemCount() {


    return breadModelClassList.size()+breadModelClass2List.size();
}


public class Viewholder extends RecyclerView.ViewHolder{

    private ImageView imageView;

    private TextView title;

    private TextView price;

    private TextView subtitle;

    private Spinner qty_spinner;

    public Viewholder(@NonNull View itemView) {

        super(itemView);

        imageView = itemView.findViewById(R.id.product_imageview);

        title = itemView.findViewById(R.id.product_title);

        price = itemView.findViewById(R.id.product_price);

        subtitle = itemView.findViewById(R.id.product_subtitle);

        qty_spinner = itemView.findViewById(R.id.bread_spinner);

    }

    private void setData(final String resource, String titleText, int priceText, String descText, String subText){

        Picasso.get().load(resource).networkPolicy(NetworkPolicy.OFFLINE).into(imageView, new Callback() {
            @Override
            public void onSuccess() {




            }

            @Override
            public void onError(Exception e) {

                Picasso.get().load(resource).into(imageView);
            }
        });

        title.setText(titleText);

        price.setText(String.valueOf(priceText));

        subtitle.setText(subText);

        final List<String> qty = new ArrayList<>();

        qty.add(descText);

        ArrayAdapter<String> qtyAdapter = new ArrayAdapter<>(context,R.layout.spinner_layout,qty);

        qtyAdapter.setDropDownViewResource(R.layout.spinner_layout);

        qty_spinner.setAdapter(qtyAdapter);



    }

    private void setData1(final String resource1, String titleText1, int priceText1, String descText1, String descText2, String subText1){

        Picasso.get().load(resource1).networkPolicy(NetworkPolicy.OFFLINE).into(imageView1, new Callback() {
            @Override
            public void onSuccess() {




            }

            @Override
            public void onError(Exception e) {

                Picasso.get().load(resource1).into(imageView1);
            }
        });

        title1.setText(titleText1);

        price1.setText(String.valueOf(priceText1));

        subtitle1.setText(subText1);

        final List<String> qty = new ArrayList<>();

        qty.add(descText1);

        qty.add(descText2);

        ArrayAdapter<String> qtyAdapter = new ArrayAdapter<>(context,R.layout.spinner_layout,qty);

        qtyAdapter.setDropDownViewResource(R.layout.spinner_layout);

        qty_spinner1.setAdapter(qtyAdapter);



    }


}

public class Viewholder1 extends Viewholder{

    public Viewholder1(@NonNull View itemView) {

        super(itemView);

        imageView1 = itemView.findViewById(R.id.product_imageview1);

        title1 = itemView.findViewById(R.id.product_title1);

        price1 = itemView.findViewById(R.id.product_price1);

        subtitle1 = itemView.findViewById(R.id.product_subtitle1);

        qty_spinner1 = itemView.findViewById(R.id.bread_spinner1);


    }



}

@Override
public int getItemViewType(int position) {

    switch(position){

        case 0:

            return R.layout.bread_item_layout;

        case 1:

            return R.layout.bread_multipleview;


    }

    return 0;

}

Trial Code 2

public class BreadAdapter extends RecyclerView.Adapter<BreadAdapter.Viewholder> {


private ImageView imageView1;

private TextView title1;

private TextView price1;

private TextView subtitle1;

private Spinner qty_spinner1;

private final static int SINGLE_VIEW = 0 , DOUBLE_VIEW = 1;

private List<BreadModelClass> breadModelClassList;

private List<BreadModelClass2> breadModelClass2List;

private Context context;

public BreadAdapter(List<BreadModelClass> breadModelClassList,List<BreadModelClass2> breadModelClass2List) {

    this.breadModelClassList = breadModelClassList;

    this.breadModelClass2List = breadModelClass2List;


}

@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


    int layout;

    context = parent.getContext();

    Viewholder viewholder;

    switch (viewType){

        case SINGLE_VIEW:

            layout = R.layout.bread_item_layout;

            View singleView = LayoutInflater.from(parent.getContext()).inflate(layout,parent,false);

            viewholder = new Viewholder(singleView);

            break;

        case DOUBLE_VIEW:

            layout = R.layout.bread_multipleview;

            View doubleView = LayoutInflater.from(parent.getContext()).inflate(layout,parent,false);

            viewholder = new Viewholder1(doubleView);

            break;

            default:

                viewholder = null;

                break;

    }

    return viewholder;


}

@Override
public void onBindViewHolder(@NonNull final Viewholder viewholder, final int position) {


    switch (viewholder.getItemViewType()){

        case SINGLEVIEW:

            String resource = breadModelClassList.get(position).getImage_resource();

            String title = breadModelClassList.get(position).getTitle();

            int price = breadModelClassList.get(position).getPrice();

            String desc = breadModelClassList.get(position).getDesc();

            String subtitle = breadModelClassList.get(position).getSubtitle();

            viewholder.setData(resource,title,price,desc,subtitle);

            break;

        case DOUBLEVIEW:

            String resource1 = breadModelClass2List.get(position).getImage_resource1();

            String title1 = breadModelClass2List.get(position).getTitle1();

            int price1 = breadModelClass2List.get(position).getPrice1();

            String desc1 = breadModelClass2List.get(position).getDesc1();

            String desc2 = breadModelClass2List.get(position).getDesc2();

            String subtitle1 = breadModelClass2List.get(position).getSubtitle1();

            viewholder.setData1(resource1,title1,price1,desc1,desc2,subtitle1);

            break;

    }

    viewholder.getAdapterPosition();

}

@Override
public int getItemCount() {


    return breadModelClassList.size()+breadModelClass2List.size();
}


public class Viewholder extends RecyclerView.ViewHolder{

    private ImageView imageView;

    private TextView title;

    private TextView price;

    private TextView subtitle;

    private Spinner qty_spinner;

    public Viewholder(@NonNull View itemView) {

        super(itemView);

        imageView = itemView.findViewById(R.id.product_imageview);

        title = itemView.findViewById(R.id.product_title);

        price = itemView.findViewById(R.id.product_price);

        subtitle = itemView.findViewById(R.id.product_subtitle);

        qty_spinner = itemView.findViewById(R.id.bread_spinner);

    }

    private void setData(final String resource, String titleText, int priceText, String descText, String subText){

        Picasso.get().load(resource).networkPolicy(NetworkPolicy.OFFLINE).into(imageView, new Callback() {
            @Override
            public void onSuccess() {




            }

            @Override
            public void onError(Exception e) {

                Picasso.get().load(resource).into(imageView);
            }
        });

        title.setText(titleText);

        price.setText(String.valueOf(priceText));

        subtitle.setText(subText);

        final List<String> qty = new ArrayList<>();

        qty.add(descText);

        ArrayAdapter<String> qtyAdapter = new ArrayAdapter<>(context,R.layout.spinner_layout,qty);

        qtyAdapter.setDropDownViewResource(R.layout.spinner_layout);

        qty_spinner.setAdapter(qtyAdapter);



    }

    private void setData1(final String resource1, String titleText1, int priceText1, String descText1, String descText2, String subText1){

        Picasso.get().load(resource1).networkPolicy(NetworkPolicy.OFFLINE).into(imageView1, new Callback() {
            @Override
            public void onSuccess() {




            }

            @Override
            public void onError(Exception e) {

                Picasso.get().load(resource1).into(imageView1);
            }
        });

        title1.setText(titleText1);

        price1.setText(String.valueOf(priceText1));

        subtitle1.setText(subText1);

        final List<String> qty = new ArrayList<>();

        qty.add(descText1);

        qty.add(descText2);

        ArrayAdapter<String> qtyAdapter = new ArrayAdapter<>(context,R.layout.spinner_layout,qty);

        qtyAdapter.setDropDownViewResource(R.layout.spinner_layout);

        qty_spinner1.setAdapter(qtyAdapter);



    }


}

public class Viewholder1 extends Viewholder{

    public Viewholder1(@NonNull View itemView) {

        super(itemView);

        imageView1 = itemView.findViewById(R.id.product_imageview1);

        title1 = itemView.findViewById(R.id.product_title1);

        price1 = itemView.findViewById(R.id.product_price1);

        subtitle1 = itemView.findViewById(R.id.product_subtitle1);

        qty_spinner1 = itemView.findViewById(R.id.bread_spinner1);


    }



}

@Override
public int getItemViewType(int position) {

    switch(position){

        case SINGLEVIEW:

            return R.layout.bread_item_layout;

        case DOUBLEVIEW:

            return R.layout.bread_multipleview;


    }

    return 0;

}

Trail Error 1

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.stuffsmart, PID: 4217
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2155)
    at android.content.res.Resources.getLayout(Resources.java:1155)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at com.example.stuffsmart.BreadAdapter.onCreateViewHolder(BreadAdapter.java:67)
    at com.example.stuffsmart.BreadAdapter.onCreateViewHolder(BreadAdapter.java:28)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:696)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Trail Error 2

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.stuffsmart, PID: 4459
java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView' on a null object reference
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6795)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
    at android.view.View.layout(View.java:20672)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:696)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  • Your 2nd trial is close to be good. In your `getItemViewType` method, you just have to return `SINGLE_VIEW ` or `DOUBLE_VIEW` instead of the view ID. Because you return the view ID, you go into the default case in your `createViewHolder` method. So, the ViewHolder is null. – cesarmarch Jun 13 '19 at 17:17
  • @cesarmarch Thank you brother but i want both the views to be returned. not an or exception, can you elaborate the code. – Aliakbar rockz Jun 14 '19 at 05:59
  • Any remedy regarding this i am having a hard time clearing this one – Aliakbar rockz Jun 16 '19 at 06:56

1 Answers1

0

I think you just have to change this function from your trial 2 and it should work after that :

    @Override
    public int getItemViewType(int position) {
        return position;
    }

Let me know how it works.

By the way, you should use Glide instead of Picasso for image loading. It's much more optimized now.

cesarmarch
  • 617
  • 4
  • 13
  • i have tried this and many other alternatives i repeatedly get error such as index out of bound, and i want both the viewholders in the same recycler after enqueuing all the 49 single variant products and then enqueue the 50th product which is a double varaint. – Aliakbar rockz Jun 14 '19 at 17:26
  • also i have tried using the methods posted in stackoverflow such as [link](https://stackoverflow.com/questions/42450466/two-arraylist-one-recyclerview-adapter) – Aliakbar rockz Jun 14 '19 at 17:30
  • So you need something like that : @Override public int getItemViewType(int position) { if (position < 48) { return SINGLE_ITEM; } else { return DOUBLE_ITEM;} } Can you detail your use-case ? – cesarmarch Jun 14 '19 at 18:09
  • Can you detail your use-case or add the full error trace ? – cesarmarch Jun 14 '19 at 18:10
  • it is dynamic the items – Aliakbar rockz Jun 14 '19 at 18:11
  • Index out of bound – Aliakbar rockz Jun 14 '19 at 18:16
  • i have 51 products which are single variant and has a different layout and a model class and couple of multi-variant products with a different layout and a model class as well. the goal is to view both the variants in the single recycler in the same adapter. SIngle variant corresponds to `SINGLEVIEW` and multi-variant to `DOUBLEVIEW` – Aliakbar rockz Jun 14 '19 at 18:25
  • Alright. Your index out of bound comes from your `onBindViewHolder`. Instead of doing `breadModelClass2List.get(position)`, you should do `breadModelClass2List.get(position - breadModelClass1List.size())` – cesarmarch Jun 14 '19 at 18:34
  • have added onBindViewHolder check that and for all the get positions i have to change ah and kindly provide your mail id as we cannot comment in this thread further and then what about getItemViewType that remains the same as u mentioned `return positon` – Aliakbar rockz Jun 14 '19 at 18:38
  • The `getItemViewType` should return something like if (position < breadModelClass1List.size()) { return SINGLE_VIEW; } else { return DOUBLE_VIEW; } – cesarmarch Jun 14 '19 at 18:42